Give this a try:
$OnlineMachines = @()
$OffLineMachines = @()
$Machines = get-content c:\junk\servers.txt
$Report = @()
[array]$OnlineMachines = Test-Connection -ComputerName $machines -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |
Select-Object -Expand Address |
Sort-Object -Unique
$Machines |
ForEach-Object{
if ($OnlineMachines -notcontains $_){
$OffLineMachines += $_
}
}
$Space = Invoke-Command -ComputerName $OnlineMachines -ScriptBlock{
Remove-Item -Path "C:\Windows\Temp*" -Recurse -Force
Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
} |
$Space |
ForEach-Object{
$Report += [PSCustomObject]@{
Status = 'Online'
SystemName = $_.systemname
Drive = $_.drive
FreeSpace = $_.freespace
}
}
$OffLineMachines |
ForEach-Object{
$Report += [PSCustomObject]@{
Status = "Unavailable: Ping or WSMan failed"
SystemName = "N/A"
Drive = "N/A"
FreeSpace = "N/A"
}
}
$Report |
Export-CSV "C:\report\Space.CSV"