I have valid credentials of a Windows service account stored in $creds and want to use them to access the C:\temp\ directory on another server called remotehost. I use Invoke-Command to execute the same test twice, first on localhost (which leads to denied access) and then on remotehost (which succeeds):
Invoke-Command -ComputerName localhost -Credential $creds -ScriptBlock {
Test-Path -Path \\remotehost\C$\temp\ # access denied
}
Invoke-Command -ComputerName remotehost -Credential $creds -ScriptBlock {
Test-Path -Path \\remotehost\C$\temp\ # True
}
Can anyone explain this "access denied"? Why can I successfully connect to remotehost and execute a command there, but I cannot execute the same command from localhost directly?
Just to be sure, I also verified that the connection to localhost works:
Invoke-Command -ComputerName localhost -Credential $creds -ScriptBlock {
Test-Path -Path C:\temp # True
}
