Powershelll script to get server status and disk space details using invoke command parallely

Faizan Sayed 46 Reputation points
2021-01-14T14:13:13.747+00:00

I want to perform disk cleanup on 500 VDIs using below script and create a proper report with VDI status(online or offline) and disk space detail using below script .But here Iam not using for each loop as I want to run cleanup parallel on VDIs .If any VDI is offline or winRM not enabled the script will throw error for that VDIs ,I want to catch those VDI and put in report if they are offline or any other winrm error .I can get disk space detail for online VDI but how to get offline or error VDI status in report ???????

$AllVDI = get-content "list for500 vDI "
$Space= Invoke-Command -ComputerName $AllVDI -ScriptBlock {
Remove-Item -Path "C:\Windows\Temp*" -Recurse -Force
Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
}
$Space|select systemname,drive,freespace|export.csv "C:\report"

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,406 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2021-01-14T21:13:28.473+00:00

    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"
    

1 additional answer

Sort by: Most helpful
  1. Faizan Sayed 46 Reputation points
    2021-01-18T19:13:44.893+00:00

    Hi Rich , One query .I have one terminal server from where all my VDIs are reachable and I am connecting this terminal using invoke-command from my management server but here in report I am not getting freespace and C:# drive name for VDIs iam getting field as NA in report .I have used the below code .But when I ran directly on MyTerminalServer I am getting proper report .

    Invoke-command -computername "MyTerminalServer" -scriptblock  {
    
    $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"
    
       }