VMware Cloud Community
virtuallycool
Contributor
Contributor

new-tagassignment value cannot be null

Hi everyone,

so I am trying to simply add a tag to a series of machines. I have the tag and a vm name in a CSV. every time I run my script my output says value cannot be null. what am i doing wrong.

thanks in advance for everyone help.

myCSV data example

tag,name

1,"windows 2016"

1,"windows 2012r2"

my Script

Import-Csv -Path c:\users\name\Documents\tags.csv -UseCulture | %{

#setting variable for name from CSV

$name = $_.Name

#setting variable for tag from CSV

$tags = $_.tag

#used to test that the variables are read into memory,

#$tags

#$name

#trying to add VM name and add a tags

Get-VM -Name $name | New-TagAssignment -Tag $tags -Confirm:$false

#a different way of doing it and it did not work either Smiley Sad

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

MY Output

New-TagAssignment : 10/3/2017 2:48:30 PM        New-TagAssignment               Value cannot be null.

Parameter name: collection

At C:\Users\name\Desktop\add-tags.ps1:6 char:22

+ Get-VM -Name $name | New-TagAssignment -Tag $tags -Confirm:$false

+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag

   Assignment

17 Replies
LucD
Leadership
Leadership

Are you sure there is no blank line in your CSV file?

At the end?


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

Reply
0 Kudos
LucD
Leadership
Leadership

And did you already try like this?

Import-Csv -Path c:\users\name\Documents\tags.csv -UseCulture | %{

    $tag = Get-Tag -Name $_.tag

    Get-VM -Name $_.Name | New-TagAssignment -Tag $tag -Confirm:$false

}


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

Reply
0 Kudos
virtuallycool
Contributor
Contributor

It fails on all 212 entries in the CSV, no space, no blanks.

Reply
0 Kudos
virtuallycool
Contributor
Contributor

same results + a new error

Get-Tag : 10/3/2017 4:04:11 PM  Get-Tag         Could not load file or assembly 'vapi-client-bindings, Version=0.0.0.0,

Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

At C:\Users\name\Desktop\add-tags.ps1:11 char:12

+     $tag = Get-Tag -Name $_.tag

+            ~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Tag], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag

Get-Tag : 10/3/2017 4:04:11 PM  Get-Tag         Tag with name '13' was not found using the specified filter(s).

At C:\Users\name\Desktop\add-tags.ps1:11 char:12

+     $tag = Get-Tag -Name $_.tag

+            ~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-Tag], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Taggin

   g.GetTag

New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.

At C:\Users\name\Desktop\add-tags.ps1:12 char:51

+     Get-VM -Name $_.Name | New-TagAssignment -Tag $tag -Confirm:$fals ...

+                                                   ~~~~

    + CategoryInfo          : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Comma

   nds.Tagging.NewTagAssignment

Reply
0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you on (do a Get-PowerCLIVersion)?

If not yet there, can you upgrade to the latest 6.5.2 (which comes from the PowerShell Gallery, see Updating PowerCLI through the PowerShell Gallery)


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

Reply
0 Kudos
virtuallycool
Contributor
Contributor

lastest version. I also did the powershell gallary update with a force.

also redid the entire installation on another machine, same issue

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, let's analyse step by step.

First, can you load the PowerCLI modules

Get-Module -Name VMware* -ListAvailable | Import-Module

And are the PowerCLI modules loaded.

Get-Module -Name VMware* -ListAvailable

Then let's check if the Tag is found

Get-Tag -Name 1

And finally if the VM is found.

Get-VM -Name 'windows 2016'


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

JoergenKaare
Contributor
Contributor

Hi, the last week I got similar error on both Set-tag and Set-networkadapter.

My deployment script has been working for month  with no problem - suddenly last week I get "value cannot be null' on both statements.

Updated to latest 6.5.3.

Running vcenter 6.5 U1

Reply
0 Kudos
LucD
Leadership
Leadership

There has been a fix on 6.5.3, can you check you have that patched version?

Name            Version

----            -------

VMware.PowerCLI 6.5.3.6870460


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

Reply
0 Kudos
JoergenKaare
Contributor
Contributor

Get the patched version - but still same problem

Reply
0 Kudos
LucD
Leadership
Leadership

Do you get the same error when you don't use splatting?


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

Reply
0 Kudos
JoergenKaare
Contributor
Contributor

Yes - same error.

Version info attached.

Reply
0 Kudos
LucD
Leadership
Leadership

I would suggest to open a SR.


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

Reply
0 Kudos
JoergenKaare
Contributor
Contributor

Rewrote the Set-networkadapter to use the port-group object instead of the port-group name - solved the problem

$vpg =  get-vdportgroup -name $pg

      $vm | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup  $vpg -Confirm:$false

same for new-tagassignment - piping the vm object solved the problem.

get-vm $vmname | New-TagAssignment -Tag $vmconfig.tag

sashamonroe
Contributor
Contributor

Hi virtuallycool,

I think the reason you are getting a null value is because you are not using a loop so the $_ is returning nothing. I have a script that does this, it loops through the CSV and applies the corresponding tag.

CSV Structure:

VM,Info

VM1,Cloud

VM2,SQL

param(

$infile,

$vcenter

)

# Example: .\set-tag.ps1 -vcenter vcenter-name -infile .\test.csv

$csv = Import-Csv $infile

$creds = Get-Credential

connect-viserver $vcenter -credential $creds

foreach ($item in $csv)

{

$vm = get-vm -name $item.vm

$tag = $item.info

New-TagAssignment -Tag $tag -Entity $vm

}

Let me know if you have any questions!

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure if your assumption is correct.

The line Import-Csv -Path c:\users\name\Documents\tags.csv -UseCulture | %{ does start a ForEach-Object (alias %) loop.

Each row in the CSV will be accessible through $_ inside the ForEach-Object code block.


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

Reply
0 Kudos
str1ker
Contributor
Contributor

Big thanks !! This saved my day .. I had the Issue that the deployment-script failed on a production site but worked on test .. and this was the workaround.

I guess there is a version diverence in PS-Modules or somewhere, although both are on the same vsphere 6.7 build.

Reply
0 Kudos