Hello all,
I am failing miserably at the following task:
I have a .csv file in the following format:
VM BusinessLine Team Environment
server1 IT Infrastructure Development
server2 Retail Marketing Production
My goal is to have PowerCLI go through the lines and add the Tags to the given VM, so that at the end, 'server1' will have the tags IT, Infrastructure and Development.
I have the categories set to 'many tags per object'
I am running the following script:
#Preconfigure environment
Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"
$vcenters = 'vcenter1.domain.local','vcenter2.domain.local','vcenter3.domain.local'
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
# Connect to vCenter servers
$vcentercreds = Get-Credential
Connect-VIServer -Server server1.domain.local -AllLinked -Credential $vcentercreds
# Import list
$csv = Import-Csv -Path 'C:\temp\tags.csv'
# For each, add the value of each Category (header row) as a tag to the VMs
foreach ($VM in $csv)
{
$name = $VM.VM
$BusinessLine = $VM.BusinessLine
$team = $VM.Team
$env = $VM.Environment
$vm = Get-VM -Name $Name
New-TagAssignment -Entity $name -Tag $BusinessLine
New-TagAssignment -Entity $name -Tag $team
New-TagAssignment -Entity $name -Tag $env
}
But the script is resulting in the attached errors. I seem to be having issues getting the correct value for the tags, but cannot figure out the right format.
I've tried swtiching up variables and pipe order, but have not figured out the proper syntax.
Can anyone possibly have a look and let me know what I'm doing?
Is that really the content of your CSV file?
No commas between the column fields?
I would expect something like this
VM,BusinessLine,Team,Environment
server1,IT,Infrastructure,Development
server2,Retail,Marketing,Production
Why do you have an Add-PSSnapin in the code?
Which PowerCLI version are you using? PSSnapins were abandoned a long time ago.
What is the point of the $vcenters variable?
It is not used anywhere in the code.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You're correct, I apologize for not being more clear, my csv does have commas as follows:
VM,BusinessLine,Team,Environment
server1,IT, Infrastructure,Development
server2,Retail,Marketing,Production
the PSSnapIn and $vcenters were to prep my environment and and part of my troubleshooting (I had the $vcenters variable because of one of the errors I received that made me think that the script couldn't identify the correct vCenter, as I have 3 vCenter servers).
I have the latest version of PowerCLI
You don't need the Add-PSSnapin line when you are using the latest PowerCLI version.
You are using the $vm variable as the loop index but also to collect the Get-VM result.
That will not work.
Try something like this
$csv = Import-Csv -Path 'C:\temp\tags.csv'
# For each, add the value of each Category (header row) as a tag to the VMs
foreach ($row in $csv) {
$name = $row.VM
$BusinessLine = $row.BusinessLine
$team = $row.Team
$env = $row.Environment
$vm = Get-VM -Name $Name
New-TagAssignment -Entity $vm -Tag $BusinessLine
New-TagAssignment -Entity $vm -Tag $team
New-TagAssignment -Entity $vm -Tag $env
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for your suggestions. The suggested changes to the script resulted in similar errors to what I had before; more or less implying that the value cannot be found when trying to add the new tag using the data in the CSV or that the vcenter is different than the one that is running the task:
New-TagAssignment : 11/15/2022 1:47:11 PM New-TagAssignment The TagCisImpl - 'IS' must be managed by the same VC Server that you are using to invoke this operation.
At line:8 char:1
+ New-TagAssignment -Entity $VM -Tag $BusinessLine
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_AssertSameClient_ManagedByAnotherServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : 11/15/2022 1:47:11 PM New-TagAssignment Value cannot be found for the mandatory parameter Tag
At line:9 char:1
+ New-TagAssignment -Entity $VM -Tag $team
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-TagAssignment], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : 11/15/2022 1:47:12 PM New-TagAssignment The TagCisImpl - 'Test' must be managed by the same VC Server that you are using to invoke this operation.
At line:10 char:1
+ New-TagAssignment -Entity $VM -Tag $env
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_AssertSameClient_ManagedByAnotherServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : 11/15/2022 1:47:12 PM New-TagAssignment The TagCisImpl - 'IS' must be managed by the same VC Server that you are using to invoke this operation.
At line:8 char:1
+ New-TagAssignment -Entity $VM -Tag $BusinessLine
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_AssertSameClient_ManagedByAnotherServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : 11/15/2022 1:47:12 PM New-TagAssignment Value cannot be found for the mandatory parameter Tag
At line:9 char:1
The error seems to be clear, you are trying to assign Tags that are defined on another vCenter than the one where the VM is registered.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sure, but the beginning lines of code connected me to all vCenters (I thought this was accomplished using the -AllLinked switch):
Connect-VIServer -Server server1.domain.local -AllLinked -Credential $vcentercreds.
Additionally, I am certain that at least some of the VMs are on the vcenter server specified in the Connect-VIServer cmdlet. Also, these tags are the same on all vCenter servers, unless there is some sort of UUID diffrence.
Is there a way to do this en masse across the different vcenters? or am i going to have to break this out by vCenter? Thanks for your help.
If you are not using Enhanced Linked Mode the Tags will not be replicated afaik.
In that case you should define the Tags on each vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Again for all your help.
It looks like the missing link was the -Server switch on the New-TagAssignment cmdlet. I guess I had to spell it out for the script because once I added that, the script worked as expected.