Get-PC/Get-PC/Private/Get-FreeDiskSpace.ps1

47 lines
2 KiB
PowerShell

function Get-FreeDiskSpace {
param (
[array]$Computers
)
$sblock = {
#Write-Progress -Activity "Retrieving data from online computers" -Status $Env:COMPUTERNAME -PercentComplete 80
try {$win32_LogicalDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" } catch {$win32_LogicalDisk = $null} #| Diskspace
$CompFreeSpace = @([math]::Round($win32_LogicalDisk.FreeSpace / 1gb,2),[math]::Round($win32_LogicalDisk.Size / 1gb,2))
$free = $compFreeSpace[0]
$max = $compfreeSpace[1]
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name 'Hostname' -Value $Env:COMPUTERNAME
$obj | Add-Member -MemberType NoteProperty -Name 'Free Space' -Value $free
$obj | Add-Member -MemberType NoteProperty -Name 'Total Space' -Value $max
return $obj
}
$data = @()
$OnlineComputers = @()
$OfflineComputers = @()
$itemIndex = 0
foreach($comp in $Computers){
$itemIndex++
Write-Progress -Activity "Connecting to Computers" -Status $Env:COMPUTERNAME -PercentComplete ($itemIndex / $Computers.Count)*100
$Connection = Test-Connection -ComputerName $comp -Count 1
if($Connection){
$OnlineComputers += $comp
}
else{
$OfflineComputers += $comp
}
}
$data += Invoke-Command -ScriptBlock $sblock -ComputerName $OnlineComputers -SessionOption (New-PSSessionOption -NoMachineProfile -OpenTimeout 45000) | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName
foreach($comp in $OfflineComputers){
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name 'Hostname' -Value $comp
$obj | Add-Member -MemberType NoteProperty -Name 'Free Space' -Value 'OFFLINE'
$obj | Add-Member -MemberType NoteProperty -Name 'Total Space' -Value 'OFFLINE'
$data += $obj
}
return $data
}