Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enter PSSession在我的Powershell脚本中不起作用_Powershell_Powershell Remoting - Fatal编程技术网

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

Enter PSSession在我的Powershell脚本中不起作用,powershell,powershell-remoting,Powershell,Powershell Remoting,当我从脚本运行下面的行时,文件最终会在本地计算机上创建 $cred = Get-Credential domain\DanTest Enter-PSSession -computerName xsappb01 -credential $cred New-Item -type file c:\temp\blahxsappk02.txt exit-pssession 当我从powershell控制台单独运行每一行时,将正确创建远程会话,并在远程计算机上创建文件。有什么想法吗?这是否是一个时间问

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

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

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

exit-pssession

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

不确定这是否是一个时间问题。我怀疑这更像是Enter-PSSession调用了嵌套提示符之类的东西,而您的后续命令没有在其中执行。无论如何,我相信进入/退出PSSession是为了交互使用,而不是脚本使用。对于脚本,请使用New PSSession并将该会话实例传递到Invoke命令,例如:

$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-我明天回办公室的时候会看一看这个。这似乎奏效了。一件事是最后一行需要删除PSSession$s@DanSnell当我使用New PSSession和Invoke Command时,始终需要Remove PSSession-Session?值得添加:为了访问
Invoke Command
之前声明的变量,需要使用
-ArgumentList
参数