No_Way
Enthusiast
Enthusiast

Hi Zik,

Again thank your for the reply.

I also used wildcard in the switch, but I think the option regex is better.

For now I was able to go this far.

$vms = Get-Cluster "Cluster Test" | Get-VM

Foreach($vm in $vms) {

    $vmview = $vm | get-view

    switch -Regex ($vmview.Summary.Config.GuestFullName)

        {

        "Linux"

            {

            If ((Get-TagAssignment -Entity (Get-VM $vms)) -like '*Linux*')

                { Write-Host "VM Linux: " $vm.Name -nonewline; Write-Host " with TAG " -foreground Green }

            Else

                {Write-Host "VM Linux: " $vm.Name, $vmview.Summary.Config.GuestFullName -NoNewline; Write-Host " NO TAG " -foreground red }

            }

        "Windows"

            {

            If ((Get-TagAssignment -Entity (Get-VM $vms)) -like '*Windows*')

                { Write-Host "VM Windows: " $vm.Name -nonewline; Write-Host " with TAG " -foreground Green  }

            Else

                {Write-Host "VM Windows: " $vm.Name -nonewline; Write-Host " NO TAG " -foreground red -nonewline; Write-Host "" $vmview.Summary.Config.GuestFullName -foreground Black }

            }

           

        "Centos"

            {

            If ((Get-TagAssignment -Entity (Get-VM $vms)) -like '*Centos*')

                { Write-Host "VM Centos: " $vm.Name -nonewline; Write-Host " with TAG " -foreground Green }

            Else

                {Write-Host "VM Centos: " $vm.Name, $vmview.Summary.Config.GuestFullName -NoNewline; Write-Host " NO TAG " -foreground red }

            }

        }

    }

And after running this in my test environment I have this:

VM Linux:  VM Test 003 SUSE Linux Enterprise 11 (32-bit) NO TAG

VM Windows:  VM Test 001 with TAG

VM Linux:  VM Test 002 Red Hat Enterprise Linux 6 (32-bit) NO TAG

VM Centos:  VM Test 004 CentOS 4/5/6 (32-bit) NO TAG

Maybe I could reduce some lines and differently, but my knowledge only get until here Smiley Happy

After I will replace the lines with the text (NO TAG, or WITH TAG) with a line to add a TAG to the VM without a TAG.

Because in the first time I run the script no VMs have TAGs, but after running the same script one time a month, it should only pickup VMs with no TAGs and then add a Guest OS TAG.

Thank You again for the help

NW

Reply
0 Kudos