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
}
The error I get:
Access is denied. + CategoryInfo : PermissionDenied: (:) [Get-CimInstance], CimException + FullyQualifiedErrorId : HRESULT 0x80070005,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand + PSComputerName : tfs-build1
$PSVersionTable from the target server:
Name Value
---- -----
PSRemotingProtocolVersion 2.3
BuildVersion 10.0.14409.1018
PSVersion 5.1.14409.1018
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSEdition Desktop
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
What I tried (and did not work):
- try {} catch {}
- setting $ErrorActionPreference to Continue, Stop
- using parameter ErrorAction with values SilentlyContinue, Continue, Stop
- trap {}
Workaround:
if ((&"net" share "$shareName" | Select-Object -Skip 1 -First 1) -match "^.+\s+(?'path'.+)$")
{
$sharePath = $Matches['path']
}
Thanks for any ideas.