VMware Cloud Community
oasisinin
Enthusiast
Enthusiast
Jump to solution

New-TagAssignment erroring out

$VC = Read-Host -Prompt "Enter vCenter Name or IP"

Connect-VIServer $VC

$server = Get-Content -Path c:\users\test\Desktop\ExportList.csv

$tag = Get-Tag "Test Tag"

New-TagAssignment -Tag $tag -Entity $server

getting following error:

New-TagAssignment : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Sdk.Types.V1.VIObjectCore' required by param

eter 'Entity'. Specified method is not supported.

At C:\Users\test\Documents\Tag.ps1:5 char:36

+ New-TagAssignment -Tag $tag -Entity <<<<  $server

    + CategoryInfo          : InvalidArgument: (:) [New-TagAssignment], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

although same script works if i assign tag to just one VM at a time.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The error indicates that there is more than 1 object in the variable $server.

The New-TagAssignment cmdlet expects 1 VIObject on the Entity parameter.

What do you have in that CSV file ? Multiple entries ?

If yes, you will need to do the assigment in a loop.

Something like this

$VC = Read-Host -Prompt "Enter vCenter Name or IP"

Connect-VIServer $VC

$tag = Get-Tag "Test Tag"

Import-Csv -Path c:\users\test\Desktop\ExportList.csv -UseCulture | %{

   New-TagAssignment -Tag $tag -Entity (Get-Inventory -Name $_.Name)

}

This assumes your CSV looks like this

Name

esx1

esx2


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

The error indicates that there is more than 1 object in the variable $server.

The New-TagAssignment cmdlet expects 1 VIObject on the Entity parameter.

What do you have in that CSV file ? Multiple entries ?

If yes, you will need to do the assigment in a loop.

Something like this

$VC = Read-Host -Prompt "Enter vCenter Name or IP"

Connect-VIServer $VC

$tag = Get-Tag "Test Tag"

Import-Csv -Path c:\users\test\Desktop\ExportList.csv -UseCulture | %{

   New-TagAssignment -Tag $tag -Entity (Get-Inventory -Name $_.Name)

}

This assumes your CSV looks like this

Name

esx1

esx2


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

0 Kudos
oasisinin
Enthusiast
Enthusiast
Jump to solution

csv exactly looks same

now getting diff error

Get-Inventory : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or

empty and then try the command again.

At C:\Users\test\Documents\Tag.ps1:5 char:60

+    New-TagAssignment -Tag $tag -Entity (Get-Inventory -Name <<<<  $_.Name)

    + CategoryInfo          : InvalidData: (:) [Get-Inventory], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetInventory

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I forgot to change your Get-Content into an Import-Csv.

I updated the code above, give it another try ?


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

0 Kudos
oasisinin
Enthusiast
Enthusiast
Jump to solution

Thank You very much

Works great!

0 Kudos