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
thank you a lot . it works perfectly with the last script.
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
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
ok thank you, I will watch...
I still have a message that says that ... But no more name problem
Do all the Tags you are trying to assign already exist?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
$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
}
Try like this.
It uses a Try-Catch construct to create the Tag when it doesn't exist.
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
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;)