Automation

 View Only
Expand all | Collapse all

script powercli to Assign tag in a category to vms and update

  • 1.  script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 07:26 AM

    Hi,

    I have a script to assign a tag in a category to vms from a csv

    it works well  with powercli 6.5

    but I have to replace the tag when the tag exists

    or a powercli script to erase the tag with the category Application_Patch

    the powercli scrip is :

    Connect-viserver vcenter1 -user vcenteruser -pass vcenterpassword

    $pathDev ="C:\Scripts\Tags_patch\ressources\"

    set-location $pathDev

    $CMDBInfo = Import-CSV C:\Scripts\Tags_patch\ressources\cmdbinfo.csv

    ForEach ($item in $CMDBInfo)

    {

    $Name = $item.Name

    $Tag = $item.Application_Patch

    #Write-host "Nom SRV : " $name " Categorie"  $tag

    Write-Host ".... Assigning $Tag in Category Application_Patch to $Name "

    Get-VM $Name | New-TagAssignment -Tag $Tag

    the csv file is like this :

    "Name","Application_Patch"

    "srv1","5_Autres"

    "srv2","4_Nuit"

    Thank you for your help



  • 2.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 07:36 AM

    You could do something like this

    Connect-viserver vcenter1 -user vcenteruser -pass vcenterpassword

    $pathDev ="C:\Scripts\Tags_patch\ressources\"

    set-location $pathDev

    $CMDBInfo = Import-CSV C:\Scripts\Tags_patch\ressources\cmdbinfo.csv

    ForEach ($item in $CMDBInfo)

    {

        $Name = $item.Name

        $Tag = $item.Application_Patch

        #Write-host "Nom SRV : " $name " Categorie"  $tag

        Write-Host ".... Assigning $Tag in Category Application_Patch to $Name "

        $vm = Get-VM -Name $Name

        Get-TagAssignment -Entity $vm | where{$_.Tag.Name -eq $Tag} | Remove-TagAssignment -Confirm:$false

        New-TagAssignment -Entity $vm -Tag $Tag

    }



  • 3.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 07:59 AM

    it works tag addition but when I restart the script with another csv to replace the tag it does not replace it but adds it in the tags



  • 4.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 08:07 AM

    What is in the other CSV? The same VMs, with another Tag?
    Can you perhaps show what happens with a screenshot or the output of a Get-TagAssignment?



  • 5.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 08:12 AM

    Yes the other csv is the same vms with another tag.

    the assignment works well but it add the tag that is in the new csv,

    and what I would like is replaced only the tag of the category _patch Application

    Thank you for your help



  • 6.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 08:17 AM

    if not the simplest is to have a script that erases the tags of each vms of the category Application_patch



  • 7.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 08:24 AM

    It would probably be a lot easier when the TagCategory 'Application_Patch' had a Cardinality of Single instead of Multiple.

    Then you could only have just 1 Tag in that TagCategory assigned.

    I assume that is what you want?
    Only one Tag assigned in the 'Application_Patch' Category?

    If yes, you could do like this

    Connect-viserver vcenter1 -user vcenteruser -pass vcenterpassword

    $pathDev ="C:\Scripts\Tags_patch\ressources\"

    set-location $pathDev

    $CMDBInfo = Import-CSV C:\Scripts\Tags_patch\ressources\cmdbinfo.csv

    ForEach ($item in $CMDBInfo)

    {

        $Name = $item.Name

        $Tag = $item.Application_Patch

        #Write-host "Nom SRV : " $name " Categorie"  $tag

        Write-Host ".... Assigning $Tag in Category Application_Patch to $Name "

        $vm = Get-VM -Name $Name

        Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq 'Application_Patch'} | Remove-TagAssignment -Confirm:$false

        New-TagAssignment -Entity $vm -Tag $Tag

    }



  • 8.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:02 AM

    actually that's what I would like to do

    or otherwise delete the tags of this category Aplication_patch to restart my script that works very well.
    I just tried changing the category _patch Application cardinalite simple, it does not work. I enclose the error encountered



  • 9.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:07 AM

    Did you try the last script I provided?

    The error you see seems to be exactly what the Carnality Single should be doing, only allow one Tag in the Category to be assigned.



  • 10.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:14 AM

    yes the error that I joined, is with the last script you provided



  • 11.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:18 AM

    That is strange, because my last script didn't contain a line with 'Get-VM $Name | New-TagAssignment -Tag $Tag'.

    Can you perhaps show the script you are using?



  • 12.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:22 AM

    the script that i'm using:

    Connect-viserver vcenter01 -user vcenteruser -pass vcenterpassword

    $pathDev ="C:\Scripts\Tags_patch\ressources\"

    set-location $pathDev

    $CMDBInfo = Import-CSV C:\Scripts\Tags_patch\ressources\cmdbinfo.csv

    ForEach ($item in $CMDBInfo)

    {

        $Name = $item.Name

        $Tag = $item.Application_Patch

        #Write-host "Nom SRV : " $name " Categorie"  $tag

        Write-Host ".... Assigning $Tag in Category Application_Patch to $Name "

        $vm = Get-VM -Name $Name

        Get-TagAssignment -Entity $vm | where{$_.Tag.Category -eq 'Application_Patch'} | Remove-TagAssignment -Confirm:$false

        New-TagAssignment -Entity $vm -Tag $Tag

    }



  • 13.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:27 AM

    Now I'm really confused.

    The last error message you included doesn't come from this script.



  • 14.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:39 AM

    however it is this script that I launch, see attachment

    I have to make a mistake somewhere but I do not see where



  • 15.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 09:50 AM

    Now the error corresponds with the script you included.
    Ok, the error seems to indicate that there is a Tag in the Application_Patch Category assigned, which the script doesn't seem to remove.

    Hence the error on the New-TagAssignment.

    Can you show the Tags assigned to the VM before the script runs?

    Web Client - VM - Summary -Tags



  • 16.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 10:05 AM

    "Can you show the Tags assigned to the VM before the script runs?

    Web Client - VM - Summary -Tags"

    --> should I put this in the script? to do what ?
    or do I have to show you a print screen of the tag on a vm?

    if not have you a script to erase the tag of the category Application_patch in each vm ?



  • 17.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 10:12 AM

    Can you check if this removes all the Tags in the Tag Category 'Application_Patch' on the VMs?

    Get-VM | Get-TagAssignment | where{$_.Tag.Category -eq 'Application_Patch'} | Remove-TagAssignment -Confirm:$false



  • 18.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 10:40 AM

    the script runs without there is nothing happening



  • 19.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 10:42 AM

    Strange, and does this show the Tag associations?

    Get-VM | Get-TagAssignment | where{$_.Tag.Category -eq 'Application_Patch'



  • 20.  RE: script powercli to Assign tag in a category to vms and update
    Best Answer

    Posted Feb 06, 2018 10:44 AM

    My bad, I forgot the Name for the Tag Category.

    Try like this

    Connect-viserver vcenter1 -user vcenteruser -pass vcenterpassword

    $pathDev ="C:\Scripts\Tags_patch\ressources\"

    set-location $pathDev

    $CMDBInfo = Import-CSV C:\Scripts\Tags_patch\ressources\cmdbinfo.csv

    ForEach ($item in $CMDBInfo)

    {

        $Name = $item.Name

        $Tag = $item.Application_Patch

        #Write-host "Nom SRV : " $name " Categorie"  $tag

        Write-Host ".... Assigning $Tag in Category Application_Patch to $Name "

        $vm = Get-VM -Name $Name

        Get-TagAssignment -Entity $vm | where{$_.Tag.Category.Name -eq 'Application_Patch'} | Remove-TagAssignment -Confirm:$false

        New-TagAssignment -Entity $vm -Tag $Tag

    }



  • 21.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 06, 2018 10:58 AM

    thank you a lot . it works perfectly with the last script.



  • 22.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 10:00 AM

    Hello, I just tried this script, unfortunately it shows me the following error message :

    Apparently my category Name in my CSV is misinformed? Yet there are the names of my machines ... I do not understand, do you know what I can do? Thank you in advance for your answer.

    Marion



  • 23.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 10:30 AM

    I think the error refers to the VM Name in your CSV, not the Category Name



  • 24.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 10:33 AM

    ok thank you, I will watch...



  • 25.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 01:44 PM

    I still have a message that says that ... But no more name problem



  • 26.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 01:58 PM

    Do all the Tags you are trying to assign already exist?



  • 27.  RE: script powercli to Assign tag in a category to vms and update

    Posted Feb 18, 2019 02:05 PM

    No. In fact I want to add the names of the owners of the machines.

    So I have the category