6

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.

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

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
PowerShell
  • 1,991
  • 8
  • 35
  • 57

1 Answers1

14

This example gets listing from remote computer's C drive and assigns it into a local variable. So tune your VMM script accordingly.

$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
mhenry1384
  • 7,538
  • 5
  • 55
  • 74
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • 4
    The key here is that Enter-PSSession is an interactive session with no connection to your local session. So you should use Invoke-Command rather than entering the remote session. – JasonMArcher Jun 30 '11 at 15:56