PowerCLI

 View Only
  • 1.  Get VM Generation ID from powershell

    Posted Feb 15, 2019 05:28 PM

    I need to monitor a vm for id changes that can break a license server for a software call tableau we use. I am already checking the uuid and mac address, but they say they also look at the VM-generation id,in the vmx file its vm.genid I believe

    does anyone know what property this is.



  • 2.  RE: Get VM Generation ID from powershell
    Best Answer

    Posted Feb 15, 2019 05:34 PM

    Try with

    Get-AdvancedSetting -Entity $vm -Name 'vm.genid'

    Get-AdvancedSetting -Entity $vm -Name 'vm.genidx'



  • 3.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 06:14 PM

    Thanks, I forgot about that one. Do you think there is anyway to improve this? So far it works and it pretty quick I'm going to try and find ways to change these manually. I'm planning on adding custom monitor to the vms that use licensing based on this.

    $vmname="name of vm"

    $olduuid=""

    $oldmacs=""

    $olddiskuuid=""

    $oldvmgenid=""

    $oldvmgenidx=""

    if((Test-Path C:\users\sjesse\$vmname-uuid.xml))

    {

        $olduuid=Import-Clixml C:\users\sjesse\$vmname-uuid.xml

        $oldmacs=Import-Clixml C:\users\sjesse\$vmname-macs.xml

        $olddiskuuid=Import-Clixml C:\users\sjesse\$vmname-diskuuid.xml

        $oldvmgenid=Import-Clixml C:\users\sjesse\$vmname-genid.xml

        $oldvmgenidx=Import-Clixml C:\users\sjesse\$vmname-genidx.xml

    }

    #get vm uuid

    $vm=get-vm $vmname| get-view | select @{l="uuid";e={$_.config.instanceuuid}}

    $uuid=$vm.uuid

    #get vm mac addresses

    $macaddress= get-vm $vmname | Get-NetworkAdapter | select -ExpandProperty MacAddress

    #get vm disk uuids

    $diskuuid= get-vm $vmname | Get-HardDisk |

    Select @{N='VM';E={$_.Parent.Name}},

        @{N='Harddisk';E={$_.Name}},

        @{N='Uuid';E={$_.ExtensionData.Backing.Uuid}}

    #get vm gen ids

    $vmgenid=Get-AdvancedSetting -Entity $vmname -Name "Vm.genid"

    $vmgenidx=Get-AdvancedSetting -Entity $vmname -Name "Vm.genidx"

    if($olduuid -ne $uuid)

    {

        Write-Host $vmname "uuid changed from $olduuid to $uuid"

        $olduuid

        $uuid

    }

    if(@(Compare-Object $macaddress $oldmacs -SyncWindow 0).Length -ne 0)

    {

        Write-Host $vmname "a mac address changed"

    }

    if(@(Compare-Object $diskuuid $olddiskuuid -SyncWindow 0).Length -ne 0)

    {

        Write-Host $vmname "a disk uuid changed"

    }

    if(@(Compare-Object $vmgenid.Value $oldvmgenid.Value -SyncWindow 0).Length -ne 0)

    {

        Write-Host $vmname "Generiation ID changed"

    }

    if(@(Compare-Object $vmgenidx.Value $oldvmgenidx.Value -SyncWindow 0).Length -ne 0)

    {

        Write-Host $vmname "Generiation ID changed"

    }

    $uuid | Export-Clixml C:\users\sjesse\$vmname-uuid.xml

    $macaddress | Export-Clixml C:\users\sjesse\$vmname-macs.xml

    $diskuuid | Export-Clixml C:\users\sjesse\$vmname-diskuuid.xml

    $vmgenid | Export-Clixml C:\users\sjesse\$vmname-genid.xml

    $vmgenidx | Export-Clixml C:\users\sjesse\$vmname-genidx.xml



  • 4.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 06:41 PM

    I would do something like this.

    Less code and easier to add/remove entries to be checked.

    $vmname="MyVM"

    $fileName = "C:\Temp\$($vm.Name)-id.xml"

    $vm = Get-VM -Name $vmname


    $tab = @{

       uuid = $vm.ExtensionData.Config.InstanceUuid

       mac = (Get-NetworkAdapter -VM $vm).MacAddress

       diskuuid = (Get-HardDisk -VM $vm).ExtensionData.Backing.Uuid

       genid = (Get-AdvancedSetting -Entity $vm -Name "Vm.genid").Value

       genidx = (Get-AdvancedSetting -Entity $vm -Name "Vm.genidx").Value

    }


    if((Test-Path -Path $fileName))

    {

       $tabOld = Import-Clixml -Path $fileName

     

       $tab.GetEnumerator() |

       ForEach-Object -Process {

       if($tab."$($_.Name)" -ne $tabOld."$($_.Name)"){

       Write-Host "$($vm.Name) $($_.Name) changed"

       }

       }

    }


    $tab | Export-Clixml -Path $fileName



  • 5.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 06:47 PM

    Thanks, that's fantasic, less code is better :smileyhappy:. That will probably work out better for the monitors I need to make.



  • 6.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 07:03 PM

    I do have one worry though, what if the VM has more than 1 harddisk or 1 vNIC?



  • 7.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 07:10 PM

    Thats the purpose  of the Export-Clixml command.it the xml file it makes looks like this

    <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">

      <Obj RefId="0">

        <TN RefId="0">

          <T>Selected.VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.FlatHardDiskImpl</T>

          <T>System.Management.Automation.PSCustomObject</T>

          <T>System.Object</T>

        </TN>

        <MS>

          <S N="VM">vm</S>

          <S N="Harddisk">Hard disk 1</S>

          <S N="Uuid">6000C291-e845-56f8-59be-b8d7006053fe</S>

        </MS>

      </Obj>

      <Obj RefId="1">

        <TNRef RefId="0" />

        <MS>

          <S N="VM">vm</S>

          <S N="Harddisk">Hard disk 2</S>

          <S N="Uuid">6000C296-de3a-38db-65a7-7fa89d4f58cc</S>

        </MS>

      </Obj>

    </Objs>

    its also why the compare-object  was needed because these reimport as arrays. If the compare-object result is more then 0 there is a difference bteween the two.



  • 8.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 07:19 PM

    Than you probably have to change my comparison.



  • 9.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 07:31 PM

    Yeah, I didn't get any errors, but the comparison always failed. I read thats because -ne and -eq compares the 1st row of the array against the complete other array. I changed

      

              if($tab."$($_.Name)" -ne $tabOld."$($_.Name)"){

    to

       if(@(Compare-Object $tab."$($_.Name)" $tabOld."$($_.Name)" -SyncWindow 0).Length -ne 0){

    its working as expected.



  • 10.  RE: Get VM Generation ID from powershell

    Posted Feb 15, 2019 07:33 PM

    Great.