2011-06-30 23 views
5

Tôi có một tập lệnh để lấy thông tin ổ cứng ảo từ vmm, im thực hiện nó từ xa từ máy chủ, hiện tại im không thể nhận giá trị biến ngoài pssession trong máy chủ cục bộ, bạn hãy giúp tôi với việc đạt được như vậy.Làm thế nào để có được một đầu ra biến từ pssession từ xa

PS C:\Windows\system32> enter-pssession iscvmm02 
[iscvmm02]: PS C:\Users\su\Documents>Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager 
[iscvmm02]: PS C:\Users\su\Documents>$hide= Get-VMMServer -ComputerName "iscvmm02.corp.avanade.org" 
[iscvmm02]: PS C:\Users\su\Documents>$VM = Get-VM | where { $_.ComputerNameString -contains "idpsm02.corp.air.org" } 
[iscvmm02]: PS C:\Users\su\Documents>$harddisk=$VM.VirtualHardDisks 
[iscvmm02]: PS C:\Users\su\Documents>$h=$harddisk.length 
[iscvmm02]: PS C:\Users\su\Documents>for($i=0;$i-lt$h;$i++){ 
    New-Variable -Name "HardDiskType_$i" -value $harddisk[$i].vhdtype 
    New-Variable -Name "HardDiskLocation_$i" -value $harddisk[$i].Location 
} 
[iadpscvmm02]: PS C:\Users\su\Documents>Exit-PSSession 
PS C:\Windows\system32>$harddisktype_0 
PS C:\Windows\system32>$harddisklocation_0 

như bạn có thể nhìn thấy cả hai giá trị null cho biến đầu ra của, im không thể giữ lại các giá trị

Trả lời

10

Ví dụ này được niêm yết từ ổ C máy tính từ xa và gán nó vào một biến địa phương. Vì vậy, điều chỉnh kịch bản VMM của bạn cho phù hợp.

$session = New-PSSession -ComputerName RemoteSystem 
Invoke-Command -Session $session -ScriptBlock { $remoteC = gci c:\ } 
# This shouldn't print anything. 
$localC 
# Print the result on remote computer an assing its output to localC variable 
$localC = Invoke-Command -Session $session -ScriptBlock { $remoteC } 
# Print the local variable, it should contain remoteC data. 
$localC 
+4

Chìa khóa ở đây là Enter-PSSession là phiên tương tác không có kết nối với phiên cục bộ của bạn. Vì vậy, bạn nên sử dụng Invoke-Command thay vì nhập phiên từ xa. – JasonMArcher

+0

cảm ơn @jasonmarcher – PowerShell

Các vấn đề liên quan