Archive for the “-eq” Category

Dung K Hoang has an excellent series of blog posts on how to manage Hyper-V with WMI.

I have taken a couple of his scripts and built a function that takes input from the pipeline and gives me an object that has three properties, a HostServer, VirtualMachine, and a Switch.

   1: function Get-HyperVInfo {
   2:     begin {
   3:         $VmSwitchinfo = @();
   4:     }
   5:     process {
   6:         $computer = $_
   7:         $ListofVMs = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName <> Name" -computer $computer
   8:         $ListofSwitches = gwmi -namespace root\virtualization Msvm_VirtualSwitch -computer $computer
   9:         $ListofSwitchPorts = gwmi -namespace root\virtualization Msvm_SwitchPort  -computer $computer
  10:         foreach ($Switch in $ListofSwitches)
  11: {
  12:             $SwitchGUID = $Switch.Name
  13:             $SwitchDisplayName = $Switch.ElementName
  14:             $PortsOnSwitch = $ListofSwitchPorts | where {$_.SystemName -match $SwitchGUID} 
  15:  
  16:             foreach ($Port in $PortsOnSwitch)
  17: {
  18:                 $PortPath = $Port.__PATH
  19:                 $ListofConnections = gwmi -namespace root\virtualization Msvm_ActiveConnection -computer $computer
  20:                 $a = $ListofConnections | where {$_.Antecedent -like $PortPath}
  21:                 if ($a -ne $NULL)
  22: {
  23:                     $LANEndPoint = $a.Dependent 
  24:                     foreach ($VM in $ListofVMs)
  25: {
  26:                         $VMGUID = $VM.Name
  27:                         $VMDisplayName = $VM.ElementName
  28:                         if ($LanEndPoint -like "*$VMGUID*")
  29: {
  30:  
  31:                             $vminfo = "" |Select-Object VirtualMachine ,HostServer, switch
  32:                             $vminfo.Switch = $SwitchDisplayName
  33:                             $vminfo.VirtualMachine = $VMDisplayName
  34:                             $vminfo.HostServer = $_
  35:                             $vmswitchinfo += $vminfo
  36:  
  37:                         }
  38:                     }
  39:                 }
  40:             }
  41:         } 
  42:  
  43:  
  44:     }
  45:     end {
  46:     $vmswitchinfo
  47:     }
  48: }

To use this function you can just do something like this

“HVSERVER1″,”HVSERVER2″,”HVSERVER3″ | Get-HypervInfo

I have used this script to get a bunch of information on all the VM’s in my Hyper V Cluster. Note that this is just using WMI so I can query my Server Core machines that are running Hyper V.

Many Many thanks to Dung for this info. I would not have even been able to get started without his help.

Comments No Comments »

Holy Schnikees there are a lot of operators in Powershell! This may not be an exact count  but looking through the help file it looks like there are 57 operators!  I am using the CTP – Powershell V2 -  which introduced a handful of new operators, particularly  -join and -split.

I’d like to start a series on some of the operators and how they can be used. I’d like to start with the most basic of operators, the -eq operator. This is the equality operator.

Note that this is different than the assignment operator “=” which is used to assign values to variables.

In a lot of programming languages, the “==” operator is used for equality.  Here’s a quick example.

   1: PS C:\Users\andys> 1 -eq 1

   2: True

   3: PS C:\Users\andys> 2 -eq 3

   4: False

This is pretty normal, (and boring). Do notice that this returns the boolean value True.

But what about if we compare an array  to an integer.

   1: PS C:\Users\andys> function new-array {,$args}

   2: PS C:\Users\andys> $i = new-array 1 2 3 4 5

   3: PS C:\Users\andys> $i

   4: 1

   5: 2

   6: 3

   7: 4

   8: 5

   9: PS C:\Users\andys> $i -eq 3

  10: 3

Interesting that the value that is returned is the value of the item in the array that was a match. It was NOT a boolean. You can read about my new-array function here for more info, but it allows you to create an array with a lot less typing.

The same is true with strings.

   1: PS C:\Users\andys> $s = new-array one two cat dog

   2: PS C:\Users\andys> $s -eq "cat"

   3: cat

   4: PS C:\Users\andys> $s -eq "not-here"

   5: PS C:\Users\andys>

However, if we use this in an if statement, it will get converted to a boolean value.

   1: PS C:\Users\andys> if ($s -eq "cat") {return $true} else {return $false}

   2: True

   3: PS C:\Users\andys> if ($s -eq "no") {return $true} else {return $false}

   4: False

   5: PS C:\Users\andys>

We can use an implicit cast to show this:

   1: PS C:\Users\andys> [bool]($s -eq "cat")

   2: True

   3: PS C:\Users\andys> [bool]($s -eq "not here")

   4: False

   5: PS C:\Users\andys>

I am continuously amazed at how deep you can go with exploring Powershell and looking at subtle nuances such as these. It’s really pretty awesome to think about how all these operators just work as you expect, given the context you are using them in.

 

Andy

Comments No Comments »