I cannot seem to get this try-catch to work. I am sure it is something simple, but my brain is just too fried at this very moment. PLEASE HELP!
param(
[String[]]$RemoteServicesVMs = ('VmThatThrowsError')
)
function getWinServiceStatus
{
#Get-WmiObject "win32_service"
try{
Get-WmiObject "win32_service" | Where-Object {$_.startname -notlike "NT*" -and $_.startname -notlike "local*" } | Format-Table -property PSComputerName, name, state, status, startname
}
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
}