I am trying to determine local path for given netshare on remote computer. For admin users all runs as expected, for non admin users the script terminates, because I can not figure out how to catch the exception.
Code (much simplified):
$credential = Get-credential $env:USERNAME
$session = New-PSSession -ComputerName tfs-build1 -EnableNetworkAccess -Credential $credential -Authentication Credssp
Invoke-Command -Session $session {
$shareName = 'Projects'
# ... snip
try {
$sharePath = (Get-CimInstance -ClassName Win32_Share -Filter "name like `"$shareName`"").Path
}
catch {
# can not get here
Write-Warning $_
}
# ... snip
}
If I understand correctly, your script opens a remote session and then performs a task, but depending on the user’s credentials that task may fail on the remote target. The failure causes a terminating error for the remote session and results in the entire script terminating. You want the script to handle the remote session error, display a warning on localhost, and continue executing.
First off, your Write-Warning at line 12 does not work because it is inside the remote session. If it produces a warning output, that output will only be shown on the remote target and not localhost.
This is effectively unsolvable. The remote session does not actively share information with the local session that initiated it. However, it is possible to capture the error using the method recommended here: powershell - Handle errors in ScriptBlock in Invoke-Command Cmdlet - Stack Overflow and then use that to create a warning message at localhost after the remote session closes.
What exactly happens if you apply -ErrorAction Continue to Get-CimInstance? Does your script terminate because of the Get-CimInstance error, or does it terminate after because it does not have the expected output of the remote session?
the only thing i can say is that the error HRESULT 0x80070005 seems to be related to the permission the user (don’t) have to access,
i faced the same error with Get-WmiObject, which worked with administrator rights but didn’t work when issueing -AsJob parameter (again with Administrator rights)
I my research trying to figure this error out and eventually fixing it I only discovered how to give the right permissions to users that
use remoting, for me it didn’t work because Administrator user has permissions already.
If you didn’t check yet, you can search for DCOM and WMI permission related to the user you are using:
Running (WinKey+R) dcomcnfg you can verify the permissions that every user have
I only found few blurry information for my problem, but i hope this will be usefull to you