Last active
February 23, 2023 13:02
-
-
Save Windos/5e33d33f8c3bedd1962f089ed240701b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules PoshRSJob, PoshPctBar, ActiveDirectory | |
function New-StorageReport { | |
<# | |
.SYNOPSIS | |
Creates a new Server Storage Report. | |
.DESCRIPTION | |
The New-StorageReport function creates a new Storage Report of local disks on all servers in a specific Organizational Unit in Active Directory. | |
.INPUTS | |
None. | |
.OUTPUTS | |
PSCustomObject | |
.NOTES | |
Please remember to edit the $OrgUnit variable before running this function. | |
.EXAMPLE | |
PS C:\> New-StorageReport | |
This command will generate a storage report and output the result to the PowerShell host. | |
.EXAMPLE | |
PS C:\> $Report = New-StorageReport | |
This command will generate a storage report and store the output in a variable for future sorting, filtering, or other usage. | |
.EXAMPLE | |
PS C:\> New-StorageReport | Select-Object * | Export-CSV -Path C:\Temp\StorageReport.csv -NoTypeInformation | |
This command will generate a storage report and export the result to a CSV file for manual manipulation. | |
#> | |
$JobBlock = { | |
if (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) { | |
$DefaultDisplaySet = 'ComputerName', 'DriveLetter', 'SizeGB', 'Usage' | |
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’, [String[]] $DefaultDisplaySet) | |
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet) | |
$ComputerName = $_.Name | |
$ManagedBy = ($_.ManagedBy -split ",*..=")[1] | |
try { | |
$WmiSplat = @{ | |
ComputerName = $ComputerName | |
Class = 'Win32_LogicalDisk' | |
Filter = 'DriveType = 3 AND VolumeName != "RESERVED_PAGING_FILE"' | |
Property = 'DeviceID', 'FreeSpace', 'Size', 'VolumeName' | |
ErrorAction = 'Stop' | |
} | |
$Disks = Get-WmiObject @WmiSplat | |
foreach ($Disk in $Disks) { | |
$PctUsed = ($Disk.Size - $Disk.FreeSpace) / $Disk.Size | |
$DiskObj = [PSCustomObject] [Ordered] @{ | |
ComputerName = $ComputerName | |
ManagedBy = $ManagedBy | |
DriveLetter = $Disk.DeviceID | |
VolumeName = $Disk.VolumeName | |
SizeRemaining = $Disk.FreeSpace | |
SizeRemainingGB = [Math]::Round($Disk.FreeSpace / 1GB, 2) | |
Size = $Disk.Size | |
SizeGB = [Math]::Round($Disk.Size / 1GB, 2) | |
Usage = New-PercentBar -Percent $PctUsed -BarCharacter '▓' | |
} | |
$DiskObj | Add-Member MemberSet PSStandardMembers $PSStandardMembers | |
$DiskObj | |
} | |
} catch { | |
$DiskObj = [PSCustomObject] [Ordered] @{ | |
ComputerName = $ComputerName | |
ManagedBy = $ManagedBy | |
DriveLetter = $null | |
VolumeName = $null | |
SizeRemaining = $null | |
SizeRemainingGB = $null | |
Size = $null | |
SizeGB = $null | |
Usage = $_.Exception.Message | |
} | |
$DiskObj | Add-Member MemberSet PSStandardMembers $PSStandardMembers | |
$DiskObj | |
} | |
} | |
} | |
$OrgUnit = 'OU=servers,OU=computers,OU=corp,DC=example,DC=com' | |
$Servers = Get-ADComputer -Filter {(OperatingSystem -like '*Server*') -and (Enabled -eq $True)} -SearchBase $OrgUnit -Properties ManagedBy | |
$Servers | Select * | Start-RSJob -ScriptBlock $JobBlock -Name {$_.Name} | Wait-RSJob -ShowProgress | Receive-RSJob | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment