Guest User

Untitled

a guest
Oct 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #Requires -Modules PoshRSJob, PoshPctBar, ActiveDirectory
  2. function New-StorageReport {
  3. <#
  4. .SYNOPSIS
  5. Creates a new Server Storage Report.
  6. .DESCRIPTION
  7. The New-StorageReport function creates a new Storage Report of local disks on all servers in a specific Organizational Unit in Active Directory.
  8. .INPUTS
  9. None.
  10. .OUTPUTS
  11. PSCustomObject
  12. .NOTES
  13. Please remember to edit the $OrgUnit variable before running this function.
  14. .EXAMPLE
  15. PS C:\> New-StorageReport
  16. This command will generate a storage report and output the result to the PowerShell host.
  17. .EXAMPLE
  18. PS C:\> $Report = New-StorageReport
  19. This command will generate a storage report and store the output in a variable for future sorting, filtering, or other usage.
  20. .EXAMPLE
  21. PS C:\> New-StorageReport | Select-Object * | Export-CSV -Path C:\Temp\StorageReport.csv -NoTypeInformation
  22. This command will generate a storage report and export the result to a CSV file for manual manipulation.
  23. #>
  24.  
  25. $JobBlock = {
  26. if (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) {
  27. $DefaultDisplaySet = 'ComputerName', 'DriveLetter', 'SizeGB', 'Usage'
  28. $DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’, [String[]] $DefaultDisplaySet)
  29. $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
  30.  
  31. $ComputerName = $_.Name
  32. $ManagedBy = ($_.ManagedBy -split ",*..=")[1]
  33.  
  34. try {
  35. $WmiSplat = @{
  36. ComputerName = $ComputerName
  37. Class = 'Win32_LogicalDisk'
  38. Filter = 'DriveType = 3 AND VolumeName != "RESERVED_PAGING_FILE"'
  39. Property = 'DeviceID', 'FreeSpace', 'Size', 'VolumeName'
  40. ErrorAction = 'Stop'
  41. }
  42.  
  43. $Disks = Get-WmiObject @WmiSplat
  44.  
  45. foreach ($Disk in $Disks) {
  46. $PctUsed = ($Disk.Size - $Disk.FreeSpace) / $Disk.Size
  47.  
  48. $DiskObj = [PSCustomObject] [Ordered] @{
  49. ComputerName = $ComputerName
  50. ManagedBy = $ManagedBy
  51. DriveLetter = $Disk.DeviceID
  52. VolumeName = $Disk.VolumeName
  53. SizeRemaining = $Disk.FreeSpace
  54. SizeRemainingGB = [Math]::Round($Disk.FreeSpace / 1GB, 2)
  55. Size = $Disk.Size
  56. SizeGB = [Math]::Round($Disk.Size / 1GB, 2)
  57. Usage = New-PercentBar -Percent $PctUsed -BarCharacter '▓'
  58. }
  59.  
  60. $DiskObj | Add-Member MemberSet PSStandardMembers $PSStandardMembers
  61. $DiskObj
  62. }
  63. } catch {
  64. $DiskObj = [PSCustomObject] [Ordered] @{
  65. ComputerName = $ComputerName
  66. ManagedBy = $ManagedBy
  67. DriveLetter = $null
  68. VolumeName = $null
  69. SizeRemaining = $null
  70. SizeRemainingGB = $null
  71. Size = $null
  72. SizeGB = $null
  73. Usage = $_.Exception.Message
  74. }
  75.  
  76. $DiskObj | Add-Member MemberSet PSStandardMembers $PSStandardMembers
  77. $DiskObj
  78. }
  79. }
  80. }
  81.  
  82. $OrgUnit = 'OU=servers,OU=computers,OU=corp,DC=example,DC=com'
  83. $Servers = Get-ADComputer -Filter {(OperatingSystem -like '*Server*') -and (Enabled -eq $True)} -SearchBase $OrgUnit -Properties ManagedBy
  84. $Servers | Select * | Start-RSJob -ScriptBlock $JobBlock -Name {$_.Name} | Wait-RSJob -ShowProgress | Receive-RSJob
  85. }
Add Comment
Please, Sign In to add comment