2
我有一個PowerShell腳本來創建新的PowerShell會話。我需要將本會話中創建的衆多變量中的一個變量返回到本地/呼叫會話。這裏是我的PowerShell腳本:如何將遠程PowerShell會話的變量返回到本地PowerShell會話
param
(
[string]$user,
[string]$password
)
$secure_password = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $user, $secure_password
$new_session = New-PSSession -Credential $cred
$script = {
$var1 = 0
$var2 = a
}
Invoke-Command -Session $new_session -ScriptBlock $script
Write-Host ($var2)
當腳本執行Write-Host ($var2)
,它不爲$ VAR2打印任何價值。我怎樣才能返回$ var2到本地會話?