Enter-PSSession 在我的 Powershell 脚本中不起作用 [英] Enter-PSSession is not working in my Powershell script

查看:59
本文介绍了Enter-PSSession 在我的 Powershell 脚本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从脚本运行以下几行时,文件最终会在我的本地机器上创建.

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

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

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?

推荐答案

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

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

这篇关于Enter-PSSession 在我的 Powershell 脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆