2010-09-14 86 views
25

当我从脚本运行下面的行时,文件最终会在我的本地机器上创建。Enter-PSSession在我的Powershell脚本中不工作

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

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

exit-pssession 

当我从控制台的powershell单独运行每行的远程会话正确地创建和远程计算机上创建该文件。任何想法为什么?这是一个时间安排问题吗?

回答

52

不确定是否是时间问题。我怀疑它更像是Enter-PSSession调用类似于嵌套提示的内容,并且后续命令不在其中执行。无论如何,我相信Enter/Exit-PSSession是用于交互式使用 - 不是脚本使用。对于脚本使用新的PSSession并传递会话实例调用到命令例如: -

$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

感谢基斯 - 当我回到办公室,我要把这看看明天。 – 2010-09-14 03:30:16

+0

这似乎是伎俩。有一件事是最后一行需要删除PSSession $ s – 2010-09-14 15:47:01

+0

谢谢,我更新了答案。 – 2010-09-15 05:43:56