Hello,
I have 3 different VMware Tags created in VMware
VM-Daily
VM-Weekly
VM-Monthly
My intention is assigning this ‘Tags’ to particular VMs.
I have 3 different txt files that has all the VMs name in it
C:\VMs_List\Daily.txt
C:\VMs_List\Weekly.txt
C:\VMs_List\ Monthly.txt
Is it possible via a script that I can assign the VMware Tag ‘VM-Daily’ to all the VMs in the txt file ‘Daily.txt’ and ‘VM-Weekly’ to the VMs in ‘Weekly.txt’ and ‘VM-Monthly’ to the VMs in ‘Monthly.txt’ ?
You could something like this.
Same concept for the other 2 tags.
Get-Content -Path C:\VMs_List\Daily.txt -PipelineVariable row |
ForEach-Object -Process {
Get-VM -Name $row | New-TagAssignment -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could something like this.
Same concept for the other 2 tags.
Get-Content -Path C:\VMs_List\Daily.txt -PipelineVariable row |
ForEach-Object -Process {
Get-VM -Name $row | New-TagAssignment -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD,
Thank you for the script.
The script works fine. But here is the issue. We have multiple vCenters and they are in linked mode.
In powershell I have connected all those vCenters together and if I run this script then I am getting the below error. But if I disconnect all the vCenter (Disconnect-VIServer -Confirm:$false "*") and then connect only one vCenter then I am able to run the script. Any guess ?
New-TagAssignment : 9/17/2020 2:48:18 PM New-TagAssignment The TagCisImpl - 'VM-Daily' must be managed by the same VC Server that you are using to invoke this operation.
At line:5 char:26
+ Get-VM -Name $row | New-TagAssignment -Tag $tag -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_AssertSameClient_ManagedByAnotherServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
You seem to be trying to assign a Tag that is not defined on the vCenter where the VM is located.
Can the script create the Tag when it is not present on the vCenter?
Under which TagCategory?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD,
If I add 2 VMs in that txt file which is belongs to the vCenter: vCenter01 and,
If I just connect the vCenter ‘vCenter01' in the PowerShell and run the script it works fine.
But, if I connect the vCenter ‘vCenter01' and ‘vCenter02' together in the PowerShell and run the script it won’t work and I am getting the mentioned error.
If I add 2 VMs in that txt file which is belongs to the vCenter: vCenter02 and,
If I just connect the vCenter ‘vCenter02' in the PowerShell and run the script it works fine.
But, if I connect the vCenter ‘vCenter01' and ‘vCenter02' together in the PowerShell and run the script it won’t work and I am getting the mentioned error.
If I add 1 VM that belongs to ‘vCenter01’ and another VM that belongs to ‘vCenter02’ in that txt file and, connect the vCenter ‘vCenter01' and ‘vCenter02' together in the PowerShell and run the script it won’t work and I am getting the mentioned error.
You will need to add the Server parameter on the Get-Tag and Get-TagAssignment cmdlets.
But you didn't answer my earlier questions
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD
I am sorry, I am not sure how to do this : "You will need to add the Server parameter on the Get-Tag and Get-TagAssignment cmdlets."
Can the script create the Tag when it is not present on the vCenter?: I am sorry, I guess Mgmt wont allow to do that
Under which TagCategory?: I didnt get this question
But its ok, anyways your script works like a charm!! Thank you for that.
Just to clarify, on most PowerCLI cmdlets you can add a Server parameter.
With that parameter you can indicate on which vCenter you want that cmdlet to run.
This allows you to work with multiple connections open.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD,
Thank you for the information. I am novice in Scripting. Appreciate if you can show me an example in your above script.
Also can you please give me the commands to Remove the Tags from the VMs
You could do something like this.
Get-VM -Name MyVM -Server $srv | Get-TagAssignment -Server $srv |
where{$_.Tag.Name -eq 'VM-Daily'} |
Remove-TagAssignment -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much!!