VMware Cloud Community
CG21
Enthusiast
Enthusiast
Jump to solution

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

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

0 Kudos
30 Replies
CG21
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

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

pastedImage_0.png

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

ok thank you, I will watch...

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

pastedImage_0.png

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

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

So I have the category "proprietary" and my tags are the names of the people concerned. Only the "proprietary" category exists.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you show the script you are using, then we can add the Tag creation (if needed) at the correct location?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

$pathDev ="C:\données\"

set-location $pathDev

$CMDBInfo = Import-CSV C:\données\Inventaire.csv

ForEach ($item in $CMDBInfo)

{

    $Name = $item.Name

    $Tag = $item.Propriétaire

    Write-Host ".... Assigning $Tag in Category Propriétaire to $Name "

    $vm = Get-VM -Name $Name

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

    New-TagAssignment -Entity $vm -Tag $Tag

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

It uses a Try-Catch construct to create the Tag when it doesn't exist.

$pathDev = "C:\données\"

set-location $pathDev

$CMDBInfo = Import-CSV C:\données\Inventaire.csv

ForEach ($item in $CMDBInfo) {

   $Name = $item.Name

   $cat = Get-TagCategory -Name 'Propriétaire'

   Try {

   $tag = Get-Tag -Name $item.Propriétaire -Category $cat -ErrorAction Stop

   }

   Catch {

   $tag = New-Tag -Name $item.Propriétaire -Category $cat

   }


   Write-Host ".... Assigning $Tag in Category Propriétaire to $Name "

 

   $vm = Get-VM -Name $Name


   Get-TagAssignment -Entity $vm -Category $cat | Remove-TagAssignment -Confirm:$false

   New-TagAssignment -Entity $vm -Tag $tag

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Elahine8
Contributor
Contributor
Jump to solution

Connect-VIServer -Server 192.168.60.196 -Protocol https -User mcartalasadm -Password %MegaOzentis2014%

$pathDev ="C:\données\"

set-location $pathDev

$CMDBInfo = Import-CSV C:\données\Inventaire.csv

ForEach ($item in $CMDBInfo)

{

    $Name = $item.Name

    $Tag = $item.Tag

    Write-Host ".... Assigning $Tag in Category Propriétaire to $Name "

    $vm = Get-VM -Name $Name

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

    New-TagAssignment -Entity $vm -Tag $Tag

}

thank you very much for your answers but I just found the error in the script. I highlighted the problem in bold, keeping everything going;)

0 Kudos