开发者

Enter-PSSession is not working in my Powershell script

开发者 https://www.devze.com 2023-01-15 20:25 出处:网络
When I run the lines below from a script the file ends up being created on my local machine. $cred = Get-Credential domain\\DanTest

When I run the lines below from a script the file ends up being created on my local machine.

$cred = Get-Credential domain\DanTest
Enter-PSSession -computerName xsappb01 -credential $cred

New开发者_高级运维-Item -type file c:\temp\blahxsappk02.txt

exit-pssession

When I run each line individually from the powershell console the remote session is created correctly and the file is created on the remote machine. Any thoughts on why? Is it a timing issue is the script perhaps?


Not sure if it is a timing issue. I suspect it's more like Enter-PSSession is invoking something like a nested prompt and your subsequent commands are not executing within it. Anyway, I believe Enter/Exit-PSSession is meant for interactive use - not scripting use. For scripts use New-PSSession and pass that session instance into Invoke-Command e.g.:

$cred = Get-Credential domain\DanTest 
$s = New-PSSession -computerName xsappb01 -credential $cred
Invoke-Command -Session $s -Scriptblock {New-Item -type file c:\temp\blah.txt}
Remove-PSSession $s
0

精彩评论

暂无评论...
验证码 换一张
取 消