0

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

2

2 Answers 2

0

Do you pass your variable to remote server? It seems like the cause here.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.