VMware Cloud Community
No_Way
Enthusiast
Enthusiast

Create Guest OS list by OS

Hi

I am trying to get a list of Guest OS VMs and then change something.

I see some examples where I can use the Get-VM and also Get-View for both cases.

Example:

get-vm | Select Name,@{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},@{N="Running OS";E={$_.Guest.OsFullName}}

or just

Get-View -ViewType VirtualMachine -Property Name,Config.GuestFullName  | %{

    New-Object -TypeName PSObject -Property @{

        Name = $_.Name

        GuestOS = $_.Config.GuestFullName

    }

} | Select Name, GuestOS

Using a filter( -Filter @{"Config.GuestFullName" = "Windows*"} ) or not to list the proper Guest OS type.

But what I need, and I am not able to do is, using with the Get-View or Get-VM, removing the filter, and in a foreach check witch VM have Linux or Windows, then do an action after found the type of Guest OS.

For example, add a TAG like this: New-TagCategory –Name "Windows" -Description "VMs System Operation TAG" –Cardinality single –EntityType VirtualMachine

I can run everything individually and works fine, but my problem is to put everything in only script and with a condition for each type of Guest OS

Thanks in advance for any help.

NW

Reply
0 Kudos
4 Replies
zik
Enthusiast
Enthusiast

You could use a switch statement:

get-vm | %{

switch ($_.ExtensionData.Config.GuestFullname)

    {

        "Microsoft Windows Server 2012 (64-bit)" {"Do one thing"}

        "Red Hat Enterprise Linux 6 (64-bit)" {"Do another"}

        default {"I don't know what to do with $_"}

    }

}

Reply
0 Kudos
No_Way
Enthusiast
Enthusiast

Hi Zik


Thanks for your reply.

Your reply did put me on the right track and had some ideas how to do it

This is what I have until now

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

Foreach($vm in $vms) {

    $vmview = $vm | get-view

    If

        ($vmview.Summary.Config.GuestFullName -like "*Windows*") {

        Write-Host "VM Name: " $vm  "Windows VM"

        #New-TagAssignment –Tag "Windows"

        }

       

    elseif

        ($vmview.Summary.Config.GuestFullName -like "*Linux*"){

        Write-Host "VM Name: " $vm " Linux VM"}

        #New-TagAssignment –Tag "Linux"

       

    elseif

        ($vmview.Summary.Config.GuestFullName -like "*CentOS*"){

        Write-Host "VM Name: " $vm " Linux/CentOS VM"}

   

    else {Write-Host "No Changes "}

Now I am struggling to check if a specific TAG is already created, if not it will creates.

Still testing:

Foreach($Tags in $Tag) {

if ($Tags.Name -notlike "*Linux*" )

{

Write-Host "No TAG Founded"

New-Tag –Name "Linux" -Description "VMs System Operation TAG" –Cardinality single –EntityType VirtualMachine

Write-Host "Tag Linux Added" | Select $tags.Name

}

else {Write-Host "TAG"}

}

But I discover that New-Tag and New-TagCategory works in different PowerCli versions.

Reply
0 Kudos
zik
Enthusiast
Enthusiast

switch also has a -regex flag:

$vms = Get-Cluster | Get-VM

Foreach($vm in $vms) {

    $vmview = $vm | get-view

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

      "Windows" {"Windows"}

      "Linux" {"Linux"}

      "CentOS" {"CentOS"}

      default {"Other"}

    }

}

Tags I haven't worked with.

Reply
0 Kudos
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