【问题标题】:Enter-PSSession is not working in my Powershell scriptEnter-PSSession 在我的 Powershell 脚本中不起作用
【发布时间】:2011-04-11 22:39:05
【问题描述】:

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

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

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

exit-pssession

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

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

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

    【讨论】:

    • 谢谢 Keith - 我明天回到办公室时会看看这个。
    • 这似乎可以解决问题。一件事是最后一行需要是 Remove-PSSession $s
    • @DanSnell Remove-PSSession 在我使用 New-PSSession 和 Invoke-Command -Session 时总是需要?
    • 值得补充的是:为了访问Invoke-Command之前声明的变量,您需要使用-ArgumentList参数
    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多