gpt4 book ai didi

powershell - Enter-PSSession 在我的 Powershell 脚本中不起作用

转载 作者:行者123 更新时间:2023-12-03 07:48:27 26 4
gpt4 key购买 nike

当我从脚本运行下面的行时,文件最终会在我的本地计算机上创建。

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

New-Item -type file c:\temp\blahxsappk02.txt

exit-pssession

当我从 powershell 控制台单独运行每一行时,远程 session 已正确创建,并且文件已在远程计算机上创建。有什么想法吗?可能是脚本的时间问题?

最佳答案

不确定是否是时间问题。我怀疑这更像是 Enter-PSSession 正在调用诸如嵌套提示之类的东西,而您的后续命令不会在其中执行。无论如何,我相信 Enter/Exit-PSSession 是用于交互式使用 - 而不是脚本使用。对于脚本,请使用 New-PSSession 并将该 session 实例传递给 Invoke-Command,例如:

$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

关于powershell - Enter-PSSession 在我的 Powershell 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com