5 Replies Latest reply on Feb 14, 2020 1:33 PM by LucD

    Write-Host writing more than desired

    Al_ Novice

      This code is writing more than the desired ".... Assigning tag "$Tag" in Category "$category" to VM "$Name" ". The 2nd and 3rd lines of output are not wanted. How can I just write .... Assigning tag  test in Category environment to VM blank, without the additional lines? 

       

      $csv = Read-Host "Please enter the .CSV path\filename to import i.e. c:\tags\ad.csv"

      $category = Read-Host "Please enter the tag category i.e. agency, os, environment, etc."

      $Tag = Read-Host "Please enter the tag to assign to the VMs"

      #$pathDev = "C:\tags\"

      #set-location $pathDev

      $CMDBInfo = Import-CSV "$csv"

      ForEach ($item in $CMDBInfo)

      {

      # Write-Host "$item"

           $Name = $item.Name

      #    $Tag = "ad"

           Write-Host ".... Assigning tag "$Tag" in Category "$category" to VM "$Name" "

           $vm = Get-VM -Name $Name

           {Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq $category} | Remove-TagAssignment -Confirm:$false (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)}

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

      }

       

      Output:

       

      .... Assigning tag  test in Category environment to VM idmtestDNR

      Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq $category} | Remove-TagAssignment -Confirm:$false (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

      environment/test                         idmtestDNR

      .... Assigning tag  test in Category environment to VM DNRNXGENTEST01

      Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq $category} | Remove-TagAssignment -Confirm:$false (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

      environment/test                         DNRNXGENTEST01

      .... Assigning tag  test in Category environment to VM DNRWEBIPAWSTEST

      Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq $category} | Remove-TagAssignment -Confirm:$false (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)

      environment/test                         DNRWEBIPAWSTEST

        • 1. Re: Write-Host writing more than desired
          LucD Guru
          Community WarriorsvExpertUser Moderators

          I'm not sure where this script was created, but I have the impression the lines are a bit messed up.

          You could try

           

          $csv = Read-Host "Please enter the .CSV path\filename to import i.e. c:\tags\ad.csv"

          $category = Read-Host "Please enter the tag category i.e. agency, os, environment, etc."

          $Tag = Read-Host "Please enter the tag to assign to the VMs"

          #$pathDev = "C:\tags\"

          #set-location $pathDev

          $CMDBInfo = Import-CSV "$csv"

          ForEach ($item in $CMDBInfo)

          {

          # Write-Host "$item"

               $Name = $item.Name

          #    $Tag = "ad"

               Write-Host ".... Assigning tag "$Tag" in Category "$category" to VM "$Name" "

               $vm = Get-VM -Name $Name

               Get-TagAssignment -Entity $vm -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)|

                  where{$_.Tag.Category -eq $category} |

                  Remove-TagAssignment -Confirm:$false

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

          }

           

          Note that New-TagAssignment will produce an object, which will also appear in your output.
          If you want to avoid that line as well, pipe the output to $null

           

               New-TagAssignment -Entity $vm -Tag $Tag -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) | Out-Null

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Write-Host writing more than desired
            Al_ Novice

            Lol, this script was created by me copying and modifying code from this forum. I'm teaching myself, hence the messy code. None the Less, it does work thanks to your help.

             

            | Out-Null works. Thanks!

             

            $csv = "c:\tags\dev.csv"

            $category = "environment"

            $Tag = "dev"

            $CMDBInfo = Import-CSV "$csv"

            ForEach ($item in $CMDBInfo)

            {

                 $Name = $item.Name

                 Write-Host ".... Assigning tag "$Tag" in Category "$category" to VM "$Name" "

                 $vm = Get-VM -Name $Name

                 {Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq $category} | Remove-TagAssignment -Confirm:$false (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)} | Out-Null

                 New-TagAssignment -Entity $vm -Tag $Tag -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) | Out-Null

            }

             

            Output:

             

            .... Assigning tag  dev in Category environment to VM sdarchiveisssql01

            .... Assigning tag  dev in Category environment to VM SDSQL20

            .... Assigning tag  dev in Category environment to VM SDSQL30

            .... Assigning tag  dev in Category environment to VM SDISSWEB1

            .... Assigning tag  dev in Category environment to VM Sddwcapp01

            .... Assigning tag  dev in Category environment to VM SDWEB22

            .... Assigning tag  dev in Category environment to VM Sdoneautomate

            .... Assigning tag  dev in Category environment to VM sddwcsql00

            .... Assigning tag  dev in Category environment to VM SDSQL21

            .... Assigning tag  dev in Category environment to VM sdeta01

            .... Assigning tag  dev in Category environment to VM Sdtfsbuild1

            • 3. Re: Write-Host writing more than desired
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              No problem.

               

              I do find it strange though that in both threads you mark your own reply as the correct answer.

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Write-Host writing more than desired
                Al_ Novice

                No worries, I didn't know I wasn't supposed to do that. Perhaps the "mark as correct answer" button should not be available to me if I'm not meant to use it.

                 

                Thanks.

                • 5. Re: Write-Host writing more than desired
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  I think that is the forum SW.

                  In any case, glad to have helped with my not 'Correct Answer'

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz