Home > database >  Powershell Error Handling not working in Invoke-command block
Powershell Error Handling not working in Invoke-command block

Time:11-10

I want to print error message when service is not found or machine is offline

$csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
   
    $computers = $cs.DNSHostname
    foreach ($computer in $computers){
   Try{
        Invoke-Command -ComputerName $computer -ScriptBlock {
        $ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
        if ($null -eq $ProcessCheck) {
            Write-output "agentid-service IS not running $env:computername"
        }
        else {
            Write-output "agentid-service IS running $env:computername"
        }
      }
    }
    catch{
        Write-Warning $Error[0]
    }
  }
}

Instead, when service name is not found, i'm getting error:

Cannot find a process with the name "agentid-service". Verify the process name and call the cmdlet again.

And if connection to computer is not possible then getting:

[vm.domain.local] Connecting to remote server vm.domain.local failed with the following error message : The WinRM client cannot process the request because the server name cannot be resolved.

And script continue execution (as it should). How can i get "Catch" block to be executed ?

CodePudding user response:

You can do this in one foreach loop.

Try

$computers = (Import-Csv "C:\Users\user\Desktop\computers.csv").DNSHostname
foreach ($computer in $computers) {
    if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
        Write-Warning "Computer $computer cannot be reached"
        continue  # skip this one and proceed with the next computer
    }
    try{
        Invoke-Command -ComputerName $computer -ScriptBlock {
            $ProcessCheck = Get-Process -Name 'agentid-service' -ErrorAction Stop
            if (!$ProcessCheck.Responding) {
                "agentid-service is NOT not running on computer '$env:computername'"
            }
            else {
                "agentid-service is running on computer '$env:computername'"
            }
        }
    }
    catch{
        Write-Warning $_.Exception.Message
    }
}

CodePudding user response:

Thanks to @Theo's answer, managed to fix it:

  $csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
    $error.Clear()
    $computers = $cs.DNSHostname
    foreach ($computer in $computers){
    Try{
   
        Invoke-Command -ComputerName $computer -ScriptBlock {
        
           $ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
           if ($null -ne $ProcessCheck) {
             Write-output "agentid-service IS running $env:computername"
           }
        
               
    } -ErrorAction Stop
  }
  catch{
     if ($null -eq $ProcessCheck){
           Write-warning "agentid-service IS not running $env:computername"
          }
    Write-Warning $Error[0]
  }
   }
}
  • Related