Automation

 View Only
  • 1.  Tagging code fails on VM with incorrect OS tag value

    Posted Mar 04, 2020 10:31 PM

    This code reads the running OS and writes the OS tag to the VM. It works as expected, except when a VM is tagged with an incorrect value for OS. I've tried several methods to catch the incorrect value and replace with the correct value. Using this: "Get-TagAssignment -Category "os" | Remove-TagAssignment -Confirm:$false", only the last vm in the list is correctly tagged, all other os tags are removed.

    How can I deal with a vm that has an incorrect os tag?

    Thanks

    $csv = Read-Host "please enter the .CSV file path\filename"


    $CMDBInfo = Import-CSV "$csv"


    ForEach ($item in $CMDBInfo)


    {


        $Name = $item.Name

        $vm = Get-VM -Name $Name -PipelineVariable vm | ForEach-Object -Process {


        #Loop through each VM, get the Running OS, assign the Running OS tag


        if($vm.ExtensionData.Guest.GuestFullName){


            try{


                $tag = Get-Tag -Category $cat -Name $vm.ExtensionData.Guest.GuestFullName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) -ErrorAction Stop

               

                }


            catch{


           

                $tag = New-Tag -Name $vm.ExtensionData.Guest.GuestFullName -Category "os" -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

               

                }


            $present = Get-TagAssignment -Entity $vm -Category "os" | where{$_.Tag.Name -eq $vm.ExtensionData.Guest.GuestFullName}


                Write-Host "Assigning tag "$tag" to VM "$Name" "

              

            if(-not $present){

              

    #          Get-TagAssignment -Category "os" | Remove-TagAssignment -Confirm:$false

           

                New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

               

            }


        }


        else{


            Write-Warning -Message "Could not determine guest OS for $($vm.Name)"

           

            }


        }


    }



  • 2.  RE: Tagging code fails on VM with incorrect OS tag value

    Posted Mar 05, 2020 01:43 AM

    Al,

    If I am understanding the problem, I think the fix should be pretty straightforward.

    It looks like you grab what the os should be in $tag, and you grab what the tag currently is in $present

    I am not reproducing your whole script, just how I think this would work for you.

    $What_OS_Tag_Should_Be = $vm.ExtensionData.Guest.GuestFullName

    $Current_OS_Tag = get-tagassignment -category "os" -entity $vm

    if ($currentOSTag -notlike $WhatOSTagShouldBe)

        {

            Get-TagAssignment -Category "os" -entity $vm | Remove-TagAssignment -Confirm:$false

            New-TagAssignment -Entity $vm -Tag $WhatOSTagShouldBe -Confirm:$false

            }

    I don't use tags all the time, but I believe this should work, or be really close for what you want



  • 3.  RE: Tagging code fails on VM with incorrect OS tag value

    Posted Mar 05, 2020 07:10 AM

    I suspect your ForEach loop might be wrong.
    Try like this

    $csv = Read-Host "please enter the .CSV file path\filename"

    $CMDBInfo = Import-Csv "$csv"

    Get-VM -Name $CMDBInfo.Name -PipelineVariable vm |

    ForEach-Object -Process {

        if ($vm.ExtensionData.Guest.GuestFullName) {

            try {

                $tag = Get-Tag -Category $cat -Name $vm.ExtensionData.Guest.GuestFullName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) -ErrorAction Stop

            }

            catch {

                $tag = New-Tag -Name $vm.ExtensionData.Guest.GuestFullName -Category "os" -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

            }

            $present = Get-TagAssignment -Entity $vm -Category "os" | Where-Object { $_.Tag.Name -eq $vm.ExtensionData.Guest.GuestFullName }

            Write-Host "Assigning tag "$tag" to VM "$Name" "

            if (-not $present) {

                #          Get-TagAssignment -Category "os" | Remove-TagAssignment -Confirm:$false

                New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

            }

        }

        else {

            Write-Warning -Message "Could not determine guest OS for $($vm.Name)"

        }

    }