Hyper-V Powershell SCVMM System Center

SCVMM custom VM properties extended

One day I was working on our fabric and thought it would be nice to be able see which Logical Switch each VM was connected to. So I started to extend my good old datastores script and before I knew it, it had morphed out of control to include Logical Switch, Logical Networks, VM Networks and NIC count.

In most enterprise environments this probably wouldn’t really add much value, but in the world of multi-tenancy with many shared or customer dedicated clusters an networks, having this kind of information easily available has it’s advantages.

As a bonus, our engineers tell me that having some of these settings easily visible has saved them quite a few clicks… So surely that’s a win too right?

Here’s some examples the result looks like in our labs. Notice the old NVGRE reference Winking smile

image

image

Ok, it’s handy. But what’s the point of this?

Why I originally wanted this, was to be able to quickly filter based on VMNet, LNet or LSwitch. As demonstrated here:

 

This is a direct copy from my production SMA runbook that gets run a few times per 24 hours.

As with all my code, it comes without warranty but feel free to use, copy, link, butcher, plagiarize as much as you desire… Yes, it could probably be streamlined or optimized, but it works well and let’s be honest – who really has that kind of spare time…

Note: Make sure you have your SMA variable set for VMMServer and that the custom properties are already created.

workflow Set-SCVMMCustomProps
{
    [string]$VMMServer = Get-AutomationVariable -Name 'VMMServer'
    inlinescript 
    {
        get-scvmmserver -computername $using:VMMServer | out-null
        # The engine room
        $VMS = Get-SCVirtualMachine  
        
        Function Set-Prop ($cp,$prop,$x,$vmx)
        {
            IF ($prop.Value -ne $x) 
            {
                $setprops = Set-SCCustomPropertyValue -CustomProperty $cp -InputObject $vmx -Value $x -RunAsynchronously 
                Return $vmx.name
            }
        }
        
        $a = @()
        ForEach ($VM in $VMS)
        {
            # Clear-Variable NICS,LS,LN,VN -ErrorAction SilentlyContinue
            $naprop = Get-SCCustomProperty -Name 'NICs'
            $lsprop = Get-SCCustomProperty -Name 'Logical Switch'
            $lnprop = Get-SCCustomProperty -Name 'Logical Network'
            $vnprop = Get-SCCustomProperty -Name 'VM Network'
            
            IF($VM.MostRecentTaskUIState -ne "Running"){
                $Nets = Get-SCCustomPropertyValue -CustomProperty $naprop -InputObject $VM
                $LSwitch = Get-SCCustomPropertyValue -CustomProperty $lsprop -InputObject $VM
                $LNet = Get-SCCustomPropertyValue -CustomProperty $lnprop -InputObject $VM
                $VMNet = Get-SCCustomPropertyValue -CustomProperty $vnprop -InputObject $VM
                Clear-Variable vnics,LS,LN,VN -ErrorAction SilentlyContinue
                IF($VM.virtualnetworkadapters.vmnetwork){
                    $VM.virtualnetworkadapters.vmnetwork | select-object -Unique | foreach {$VN += $_.Name + ","}
                    $VN = $VN.TrimEnd(",")
                }ELSE{$VN = "None"}
                IF($VM.virtualnetworkadapters.logicalnetwork){
                    $VM.virtualnetworkadapters.logicalnetwork | select-object -Unique | foreach {$LN += $_.Name + ","}
                    $LN = $LN.TrimEnd(",")
                }ELSE{$LN = "VLAN $($VM.virtualnetworkadapters.vlanid)"}
                IF($VM.virtualnetworkadapters.virtualnetwork){
                    $VM.virtualnetworkadapters.virtualnetwork | select-object -Unique | foreach {$LS += $_ + ","}
                    $LS = $LS.TrimEnd(",")
                }ELSE{$LS = "Not Connected";$LN = "Not Connected";$VN = "Not Connected"}
                
                $vnics = [string]($VM.virtualnetworkadapters.count)
        
                $a += Set-Prop $lnprop $LNet $ln $vm
                $a += Set-Prop $vnprop $VMNet $vn $vm
                $a += Set-Prop $lsprop $LSwitch $ls $vm
                $a += Set-Prop $naprop $Nets $vnics $vm
            }
            ELSE
            {
                Write-Output "$($VM.Name) has a job running - skipping."
            }
        }
        
        #Refresh updated VMs
        
        IF($a)
        {
            $a | select-object -Unique | % {Write-output "Refreshing $_" ;$vm = Read-SCVirtualMachine -vm $_ -RunAsynchronously}
        }
        ELSE
        {
            Write-output "No VMs to refresh"    
        }
    }
}

Hope this helps someone

Happy SCVMM’ing 😀
Dan

Leave a Reply