Try Catch not working in Powershell Script
![]()
I modified your function param a little bit , but for your answer , I have to say since you are using notlike your get-wmiobject is not returning any error.
If data is not there also , it will show you blank. You can narrow down the issue by putting the output in the variable and displaying the output.
You should use -eq with wildcard to deal with it.
For narrowing down the issue, use this:
$erroractionpreference = stop; function getWinServiceStatusparam( [String[]]$RemoteServicesVMs = (‘VmThatThrowsError’) ) { #Get-WmiObject “win32_service” try{ $a= Get-WmiObject “win32_service” | Where-Object {$_.startname -notlike “NT*” -and $_.startname -notlike “local*” } | Format-Table -property PSComputerName, name, state, status, startname Write-Host $a } catch{ wite-host “Failed” } } $PassWordEnc = convertto-securestring $RemotePassWord -asplaintext -force $MyCred = New-Object -TypeName System.Management.Automation.PSCredential ArgumentList $RemoteUserName,$PassWordEnc foreach($RemoteServicesVM in $RemoteServicesVMs){ Invoke-Command -ComputerName $RemoteServicesVM -Port 5985 -Authentication Negotiate -ScriptBlock ${function:getWinServiceStatus} -Credential $MyCred }
Hope it helps.
