简体   繁体   English

如何从远程 pssession 中获取变量 output

[英]How to get an variable output from remote pssession

I have a script to get virtual harddisk info from vmm, im executing it remotely from a server, currently im unable to get the variable value outside of the pssession in the local host, could you please help me out with achieveing the same.我有一个从 vmm 获取虚拟硬盘信息的脚本,我从服务器远程执行它,目前我无法在本地主机的 pssession 之外获取变量值,你能帮我实现同样的目标吗?

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

as you can see both the variable output's give null value, im unable to retain the values如您所见,变量输出都给出 null 值,我无法保留这些值

This example gets listing from remote computer's C drive and assigns it into a local variable.此示例从远程计算机的 C 驱动器获取列表并将其分配给局部变量。 So tune your VMM script accordingly.因此,相应地调整您的 VMM 脚本。

$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

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

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