I have a ps script on a server, it goes round calling various servers/clients on its domain using invoke-command and passing over a script to remotely run some jobs via a COM object it uses on the called computer. On one of the servers, because the job there turns out to be a local job, when a com object is instantiated it doesnt have the property myComObject.NetworkUser.
the code is this effectively
$NetworkUser = $comobject.NetworkUser
if the job is on the server where its effectively running local I get an error
"The property 'NetworkUser' cannot be found on this object. Verify that the property exists"
This then ends up coming back into the calling scripts scope and registers as an error.
Ive tried to stop the error by checking if the NetworkUser property exists beforehand like so
if($null -eq $comobject.NetworkUser)
{
#dont try and assign it to anything
}
else
{
#use the value and go ahead and do the job
}
But this still throws the error.
If the NetworkUser property doesn't exist I just want to exit the remotely called script (as the job doesn't need to be run) and return back to the calling scope with no errors showing/being passed back.
How can I do this?
Many thanks
In the end I found a way to not get the error by using this style to check if a property exists before calling it
$myComObject= New-Object -ComObject 'comObjectofInterest'
if([bool]($myComObject.PSobject.Properties.Name -match 'NetworkUser))
{
#Use the property
}