PowerCLI

 View Only
Expand all | Collapse all

new-tagassignment value cannot be null

  • 1.  new-tagassignment value cannot be null

    Posted Oct 03, 2017 07:27 PM

    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 :smileysad:

    #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



  • 2.  RE: new-tagassignment value cannot be null

    Posted Oct 03, 2017 07:48 PM

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

    At the end?



  • 3.  RE: new-tagassignment value cannot be null

    Posted Oct 03, 2017 08:02 PM

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



  • 4.  RE: new-tagassignment value cannot be null

    Posted Oct 03, 2017 07:50 PM

    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

    }



  • 5.  RE: new-tagassignment value cannot be null

    Posted Oct 03, 2017 08:07 PM

    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



  • 6.  RE: new-tagassignment value cannot be null

    Posted Oct 03, 2017 08:15 PM

    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)



  • 7.  RE: new-tagassignment value cannot be null

    Posted Oct 04, 2017 02:29 PM

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

    also redid the entire installation on another machine, same issue



  • 8.  RE: new-tagassignment value cannot be null

    Posted Oct 04, 2017 04:30 PM

    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'



  • 9.  RE: new-tagassignment value cannot be null

    Posted Oct 21, 2017 07:35 AM

    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



  • 10.  RE: new-tagassignment value cannot be null

    Posted Oct 21, 2017 08:25 AM

    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



  • 11.  RE: new-tagassignment value cannot be null

    Posted Oct 21, 2017 09:05 AM

    Get the patched version - but still same problem



  • 12.  RE: new-tagassignment value cannot be null

    Posted Oct 21, 2017 10:59 AM

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



  • 13.  RE: new-tagassignment value cannot be null

    Posted Oct 22, 2017 07:26 AM

    Yes - same error.

    Version info attached.



  • 14.  RE: new-tagassignment value cannot be null

    Posted Oct 22, 2017 07:53 AM

    I would suggest to open a SR.



  • 15.  RE: new-tagassignment value cannot be null

    Posted Oct 22, 2017 04:09 PM

    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



  • 16.  RE: new-tagassignment value cannot be null

    Posted Nov 02, 2020 03:15 PM

    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.



  • 17.  RE: new-tagassignment value cannot be null

    Posted Dec 15, 2017 02:02 PM

    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!



  • 18.  RE: new-tagassignment value cannot be null

    Posted Dec 15, 2017 02:15 PM

    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.