I often use PowerShell remote sessions to manage windows servers remotely, generally using the following command, though the following is true for any remote powershell commands such as Invoke-Command
:
Enter-PSSession Server01
This works perfectly for almost everything local to that server, but never allows you to reach outside, e.g.:
PS C:\> Enter-PSSession Server01
[Server01]: PS C:\> Get-Item '\\Server02\Share\File'
get-item : Access is denied
What is the easiest, secure way to do a second-hop like this? I have a couple answers I'll add, but they generally have caveats.
Use Kerberos constrained delegation to allow Server01 to be able to authenticate you to Server02:
And some pros/cons from the linked Microsoft doc and myself:
Pros
Set-ADComputer -Identity 'Server02' -PrincipalsAllowedToDelegateToAccount $null
Cons
To find existing delegations (or ones you've forgotten to remove) run this to return a list:
Save a credential to a
PSSessionConfiguration
object with the following command:Then use that configuration to connect and hop:
Pros:
Invoke-Command -ComputerName Server01 -ScriptBlock {Unregister-PSSessionConfiguration 'MyUser'}
Cons:
-Force
flag toRegister-PSSessionConfiguration
)Pass credential object through remote variables:
Pros:
Cons:
$Using
variables are only available withInvoke-Command
, and not in PowerShell sessions.-credentials
flag.Copy-Item
doesn't have one, butStart-BitsTransfer
does, so it may take some digging.