从脚本运行远程会话与手动输入确切的命令序列时,我得到了不同的结果。这发生在我正在处理的一个相当复杂的脚本中,但我创建了一个很小的脚本来演示这个问题。返回的数字不应该是7-5-7,而不是7-5-5吗?
有问题的脚本
$oldMachineName = "ppal12084229"
$remoteSession = New-PSSession $oldMachineName
$xyz =7
"outside remote session"
$xyz
""
enter-PSSession $remoteSession
$xyz = 5
"inside remote session"
$xyz
""
exit-pssession
"outside remote session"
$xyz
""
remove-pssession $remoteSession当我运行脚本时,我得到以下输出:
outside remote session
7
inside remote session
5
outside remote session
5但是,当我手动输入命令时,我得到的结果是:
PS H:\> $oldMachineName = "ppal12084229"
PS H:\> $remoteSession = New-PSSession $oldMachineName
PS H:\> $xyz =7
PS H:\> $xyz
7
PS H:\> enter-PSSession $remoteSession
[ppal12084229]: PS C:\WINDOWS\system32> $xyz = 5
[ppal12084229]: PS C:\WINDOWS\system32> $xyz
5
[ppal12084229]: PS C:\WINDOWS\system32> exit-pssession
PS H:\> $xyz
7
PS H:\> remove-pssession $remoteSession为什么我会得到不同的结果?
发布于 2012-11-20 01:20:12
Enter-PSSession和Exit-PSSession是用于交互使用的。在你的脚本中尝试一下,看看你会得到什么:
$oldMachineName = "ppal12084229"
$remoteSession = New-PSSession $oldMachineName
$xyz =7
"outside remote session"
$xyz
""
invoke-command -Session $remoteSession -ScriptBlock {
$xyz = 5
"inside remote session"
$xyz
""
}
"outside remote session"
$xyz
""
remove-pssession $remoteSessionhttps://stackoverflow.com/questions/13458253
复制相似问题