簡體   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?

從腳本運行遠程會話與手動輸入確切的命令序列時,我得到了不同的結果。 這發生在我正在處理的相當復雜的腳本中,但我創建了一個很小的腳本來演示這個問題。 返回的數字不應該是 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

為什么我得到不同的結果?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM