简体   繁体   English

Powershell 2.0:从脚本运行远程会话与手动输入时获得不同的结果。 有什么不同?

[英]Powershell 2.0: Getting different results when running a remote session from a script vs typing it in manually. What's the difference?

I'm getting different results when running a remote session from a script vs typing the exact command sequence in manually.从脚本运行远程会话与手动输入确切的命令序列时,我得到了不同的结果。 This is happening with a fairly complicated script I'm working on, but I created a tiny little script to demonstrate the issue.这发生在我正在处理的相当复杂的脚本中,但我创建了一个很小的脚本来演示这个问题。 Shouldn't the numbers returned be 7-5-7, not 7-5-5?返回的数字不应该是 7-5-7,而不是 7-5-5?

The script in question有问题的脚本

$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

When I run the script, I get this output:当我运行脚本时,我得到以下输出:

outside remote session
7

inside remote session
5

outside remote session
5

But, when I type in the commands manually, I get this:但是,当我手动输入命令时,我得到以下信息:

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

Why do I get different results?为什么我得到不同的结果?

Enter-PSSession and Exit-PSSession are meant for interactive use. Enter-PSSessionExit-PSSession用于交互式使用。 Try this in your script and see what you get:在你的脚本中试试这个,看看你得到了什么:

$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 $remoteSession

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM