none
how to use a variable in a cmdlet RRS feed

  • Question

  • I am trying to do something that seems fairly simple, but I just can't seem to get it to work. I'm putting it here in the scripting forum, but it is a Hyper-v cmdlet I am using.

    I am trying to use a text file to feed the name of a virtual machine into a parameter called $VMName, then call that parameter in the start-vmfailover command to simulate a VM failover.  Here is the script I am working on.

     Param ([string]$VMName)

    $VMName = Get-Content c:\dir\file.txt

    Start-VMFailover -vmname $VMName -astest

    If I use just the first two lines and display the value of $VMName is shows the correct name from my text file.  However, when I run the whole script I get the error:

    start-vmfailover : cannot validate argument on parameter 'VMName'.  The argument is null or empty.  Supply and argument that is not null or empty then try the command again.

    I suppose this could be an issue with start-vmfailover, but I am not a powershell expert so am wondering if I am not passing the param correctly to the cmdlet.


    Mark

    Wednesday, May 28, 2014 5:39 PM

Answers

  • You are not using the CmdLet correctly.

    Parameter Set: VMSnapshot_Test
    Start-VMFailover [-VMRecoverySnapshot] <VMSnapshot> -AsTest [-AsJob] [-PassThru] [-Confirm] [-WhatIf] [ <CommonParameters>]

    Note that the parameter set wit AsTest does not have VMName as a parameter.  It is only used to test snapshots.

    Why you would choose to use Excel is a mystery.

    $vms=Get-Content c:\dir\file.txt
    foreach($vm in $vms){
         Start-VMFailover -vmname $vm -WhatIf -Verbose
    }


    ¯\_(ツ)_/¯

    Friday, May 30, 2014 5:35 PM

All replies

  • [String] means a single string. If you intend to declare an array of strings, you would use [String[]].

    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 28, 2014 5:54 PM
    Moderator
  • You wouldn't use param if you aren't passing the data to the script when you run the script, all you need here is:

    Start-VMFailover -vmname (Get-Content c:\dir\file.txt) -astest

    If you're writing the script to be reusable you could follow Bill's advice and take the $VMName = out of the script entirely and run the script like:

    .\VMFailoverScript.ps1 -VMName (Get-Content c:\dir\file.txt)

    That way your data source isn't hard-coded in the script.  

    I would expect a different error if the string declaration was causing the problem.


    I hope this post has helped!

    Wednesday, May 28, 2014 6:26 PM
  • Thanks.  I tried this but I still get the same error.

    Mark

    Thursday, May 29, 2014 3:00 PM
  • Thanks.  I tried this but I still get the same error.


    Mark

    Thursday, May 29, 2014 3:02 PM
  • What exact code are you running, and how?  The error is pretty specific and suggests that $VMName is nothing when that command is executed.  Before the Start-VMFailover command just output $VMName to be sure it contains an array of server names.  If the data type was wrong there would be a different error.

    Since you are defining the $VMName variable just before the Start-VMFailover command you don't need the param statement at all, and as Bill suggested, the way it is defined in your original code sample will force $VMName to be a single string of computernames instead of an array and it wouldn't work.  You can get rid of it altogether unless you're passing the variable to the script or function.  i.e. with this as your script

    Param ([string[]]$VMName)
    Start-VMFailover -vmname $VMName -astest

    You would call the script like this:

    .\Script.ps1 -VMName (Get-Content c:\dir\file.txt)



    I hope this post has helped!

    Thursday, May 29, 2014 3:23 PM
  • Still no go.  I am going to write this off as an issue with the start-vmfailover cmdlet.  Instead I am just running a ps command to generate a text file list of VM names and using Excel to concatenate it into a list of VM specific lines of code I can then just paste into ps.  Thanks for the help though.

    Mark

    Friday, May 30, 2014 5:12 PM
  • You are not using the CmdLet correctly.

    Parameter Set: VMSnapshot_Test
    Start-VMFailover [-VMRecoverySnapshot] <VMSnapshot> -AsTest [-AsJob] [-PassThru] [-Confirm] [-WhatIf] [ <CommonParameters>]

    Note that the parameter set wit AsTest does not have VMName as a parameter.  It is only used to test snapshots.

    Why you would choose to use Excel is a mystery.

    $vms=Get-Content c:\dir\file.txt
    foreach($vm in $vms){
         Start-VMFailover -vmname $vm -WhatIf -Verbose
    }


    ¯\_(ツ)_/¯

    Friday, May 30, 2014 5:35 PM