VMware Cloud Community
Al_
Enthusiast
Enthusiast

PowerCLI vCenter tagging - remove .CSV from process

I'm running this script to tag VMs. Is it possible to remove the  "Export-CSV D:\tags\"$EnvironmentName.csv" -NoTypeInformation -UseCulture $AgencyDBInfo = Import-CSV "$EnvironmentName.csv""  process to speed up execution?

Get-VICredentialStoreItem -File C:\Users\*********\AppData\Roaming\VMware\credstore\vicredentials.xml | %{
Connect-VIServer -Server $_.host -User $_.User -Password $_.Password
}

$environmentcsv = "d:\tags\environment.csv"

$CMDBInfo = Import-CSV "$environmentcsv"

$category = "environment"

ForEach ($item in $CMDBInfo)

{

$EnvironmentName = $item.Name

Write-Host "processing" $EnvironmentName

Get-Folder -Name $EnvironmentName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) | Get-VM | Get-View -Property @("Name") |

Select -Property Name |

Export-CSV D:\tags\"$EnvironmentName.csv" -NoTypeInformation -UseCulture

$AgencyDBInfo = Import-CSV "$EnvironmentName.csv"

ForEach ($item in $AgencyDBInfo)

{

$Name = $item.Name

$vm = Get-VM -Name $Name

$Exists = Get-TagAssignment -Entity $vm -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) | where{$_.Tag.Category.Name -eq $category}

If ( !$Exists ) {

New-TagAssignment -Entity $vm -Tag $EnvironmentName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) | Out-Null

Write-Host ".... Assigning tag "$EnvironmentName" in Category "$category" to VM "$Name" "

}
}

}

Reply
0 Kudos
13 Replies
scott28tt
VMware Employee
VMware Employee

@Al_ 

Moderator: Moved to PowerCLI Discussions, the {code} area is for SDK matters.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership

You could try something like this

 

Get-VICredentialStoreItem -File C:\Users\*********\AppData\Roaming\VMware\credstore\vicredentials.xml | % {
    Connect-VIServer -Server $_.host -User $_.User -Password $_.Password
}

$environmentcsv = "d:\tags\environment.csv"
$CMDBInfo = Import-Csv "$environmentcsv"
$category = "environment"

Import-Csv -Path $environmentcsv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
    Write-Host "processing $($row.Name)"
    Get-Folder -Name $row.Name |
    Get-VM -PipelineVariable vm |
    ForEach-Object -Process {
        try{
            Get-TagAssignment -Entity $vm  -ErrorAction Stop | 
            where{$_.Tag.Category.Name -eq $category} | 
            Out-Null
        }
        catch{
            New-TagAssignment -Entity $vm -Tag $EnvironmentName | 
            Out-Null
            Write-Host ".... Assigning tag $($row.Name) in Category $category to VM $($vm.Name)"
        }
    }
}

 


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Thanks Luc, 

It's not catching untagged VMs. I'm studying your code to try to understand it...

Regards,

Al

Reply
0 Kudos
LucD
Leadership
Leadership

I noticed the -ErrorAction Stop was on the wrong line.
I corrected the code above


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Still no luck. Executes without error, does not catch untagged VMs.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Try this version

 

Get-VICredentialStoreItem -File C:\Users\*********\AppData\Roaming\VMware\credstore\vicredentials.xml | % {
    Connect-VIServer -Server $_.host -User $_.User -Password $_.Password
}

$environmentcsv = "d:\tags\environment.csv"
$CMDBInfo = Import-Csv "$environmentcsv"
$category = "environment"

Import-Csv -Path $environmentcsv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
    $EnvironmentName = $row.Name
    Write-Host "processing $($row.Name)"
    Get-Folder -Name $row.Name |
    Get-VM -PipelineVariable vm |
    ForEach-Object -Process {
        $tag = Get-TagAssignment -Entity $vm | 
            where{$_.Tag.Category.Name -eq $category}
        if($tag -eq $null){
            New-TagAssignment -Entity $vm -Tag $EnvironmentName | Out-Null
            Write-Host ".... Assigning tag $($row.Name) in Category $category to VM $($vm.Name)"
        }
    }
}

 


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Throws null Tag parameter error:

New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\tags\lucd.ps1:18 char:48
+ ... New-TagAssignment -Entity $vm -Tag $EnvironmentName | Out-N ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

Reply
0 Kudos
LucD
Leadership
Leadership

I forgot the set the $EnvironmentName variable.
I just corrected that


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Connecting to two on-prem and one VMC vCenters...

New-TagAssignment : 1/6/2021 11:42:24 AM New-TagAssignment The TagCisImpl - 'test' must be managed by the same VC Server that you are using to invoke this oper
ation.
At D:\tags\lucd.ps1:19 char:13
+ New-TagAssignment -Entity $vm -Tag $EnvironmentName | Out ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_AssertSameClient_ManagedByAnotherServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

Reply
0 Kudos
LucD
Leadership
Leadership

That is not really an issue with the code, it seems that your list contains VMs from multiple vCenters.
And the Tag you are trying to assign does not seem to exist on the vCenter to which the VM belongs.


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Thanks Luc,

I think the tag exists. Possibly something else?

PS D:\tags> .\auth.ps1

Name Port User
---- ---- ----
ef-vcenter-03.*****.*******... 443 *****\*******
ef-vcenter-03.*****.*******... 443 *****\*******
ef-vcenter-03.*****.*******... 443 *****\*******


PS D:\tags> Get-Tag -name "test"

Name Category Description
---- -------- -----------
test environment
test environment
test environment


PS D:\tags> .\auth.ps1

Name Port User
---- ---- ----
lw-vcenter-03.*****.*******... 443 *****\*******
lw-vcenter-03.*****.*******... 443 *****\*******
lw-vcenter-03.*****.*******... 443 *****\*******


PS D:\tags> Get-Tag -name "test"

Name Category Description
---- -------- -----------
test environment
test environment
test environment


PS D:\tags> .\auth.ps1

Name Port User
---- ---- ----
****.****.14.4 443 *****\*******
****.****.14.4 443 *****\*******
****.****.14.4 443 *****\*******


PS D:\tags> Get-Tag -name "test"

Name Category Description
---- -------- -----------
test environment
test environment
test environment


PS D:\tags>

Reply
0 Kudos
LucD
Leadership
Leadership

Since the tag seems to exist in the 3 vCenters, you will probably have to make sure you are picking the correct Tag from the same vCenter the VM belongs to.
That would mean you can't use the OBN for the tag but that you would have to use a Get-Tag to pick the correct one.


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

Reply
0 Kudos
Al_
Enthusiast
Enthusiast

Thanks Luc, will do. As always, your help is greatly appreciated!

Reply
0 Kudos