$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.
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
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
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
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
Thank You very much
Works great!