none
How do I pass a variable as a parameter? RRS feed

  • Question

  • I've tried many ways but cannot seem to pass my variable to the cmdlet I am trying to use without getting an error.  I don't want to use a switch statement to duplicate code, and I can't seem to wrap all the parameters in a variable and pass to Invoke-Expression because one of the parameters contains a credential object.  Here is what my code looks like so far:

    $paramChoice = "foo"

    Some-Command -Param1 $p1 -Param2 $p2 -$paramChoice $p3

    Back story: I'm trying to use the New-WinSCPSession cmdlet that is a wrapper around WinSCP's .NET library.  One of the parameters ($paramChoice in the example) can either be -TlsHostCertificateFingerprint or -SshHostKeyFingerprint, depending on the type of server you are connecting to.  PowerShell doesn't seem to like variables being passed as the names of parameters.  Is there a good way around this?  I tried the Invoke-Expression route (saving the command and parameters in a variable), but ran into issues since I also need to pass a credential object as a parameter to New-WinSCPSession.

    Thursday, July 21, 2016 7:48 PM

Answers

  • Hi,

    well, this appears to call for splatting:

    $param = @{
        paramChoice = "foo"
    }
    
    Some-Command -Param1 $p1 -Param2 $p2 @param

    This is how you can bind parameters dynamically.

    Cheers,
    Fred

    Edit: Here a more flexible example:

    $param = @{ }
    $param["paramChoice"] = "foo"
    if ($test) { $param["Param2"] = $p2 }
    
    Some-Command -Param1 $p1 @param


    There's no place like 127.0.0.1


    • Edited by FWN Thursday, July 21, 2016 10:50 PM
    • Marked as answer by si1ic0n_gh0st Friday, July 22, 2016 12:38 PM
    Thursday, July 21, 2016 10:47 PM

All replies

  • Your code example is just wrong.

    Some-Command -Param1 $p1 -Param2 $p2 -$paramChoice $p3

    "-$paramChoice" is illegal syntax.

    Your question is very vague.  I suggest learning a bit more about basic PowerShell syntax and how objects and Parameters work before trying to sort this out.


    \_(ツ)_/

    Thursday, July 21, 2016 8:36 PM
  • Thanks for troll.  I appreciate the laugh.
    Thursday, July 21, 2016 8:53 PM
  • Thanks for troll.  I appreciate the laugh.

    You mistake this.  Your post is almost completely indecipherable.  The syntax is wrong besides other things.  Try and rethink and ask an understandable question.

    \_(ツ)_/

    Thursday, July 21, 2016 8:55 PM
  • I'll make it simpler then:  suppose I want to choose debug or verbose as my parameter...

    $opt = "debug"

    or

    $opt = "verbose"

    Then pass my choice as a parameter to Invoke-Command:

    Invoke-Command -Scriptblock $ScriptBlock -$opt

    If that is completely indecipherable then there isn't much I can do for you.

    Thursday, July 21, 2016 9:27 PM
  • Get-Process -Debug -Verbose

    help about_parameters


    \_(ツ)_/

    Thursday, July 21, 2016 9:28 PM
  • Here is how these variables work:

    function testp{
    	[cmdletbinding(SupportsShouldProcess)]
    	Param(
    		$file
    	)
    	remove-item $file
    }
    
    testp test.txt -whatif
    Note that the outer "WhatIf" is passed through to all contained calls and child calls.


    \_(ツ)_/

    Thursday, July 21, 2016 9:37 PM
  • Hi,

    well, this appears to call for splatting:

    $param = @{
        paramChoice = "foo"
    }
    
    Some-Command -Param1 $p1 -Param2 $p2 @param

    This is how you can bind parameters dynamically.

    Cheers,
    Fred

    Edit: Here a more flexible example:

    $param = @{ }
    $param["paramChoice"] = "foo"
    if ($test) { $param["Param2"] = $p2 }
    
    Some-Command -Param1 $p1 @param


    There's no place like 127.0.0.1


    • Edited by FWN Thursday, July 21, 2016 10:50 PM
    • Marked as answer by si1ic0n_gh0st Friday, July 22, 2016 12:38 PM
    Thursday, July 21, 2016 10:47 PM
  • Thank you SO MUCH!  This has made my day!  I have seen others use splatting but I didn't know you could pick and choose parameters to use it with.  I assumed that if you used it, then it had to be used for all of your parameters...and that didn't seem to jive with my PSCredential object.

    In case this helps someone in the future, here is my working script block for establishing a session with dotPS1's WinSCP PowerShell wrapper and then sending files:

    $paramCertType = @{ }
    if ($certType -eq "TLS") {
        $paramCertType["TlsHostCertificateFingerprint"] = $fingerprint
    } else {
        $paramCertType["SshHostKeyFingerprint"] = $fingerprint
    }

    $send = New-WinSCPSession `
        -HostName $hostName `
        -Credential $cred `
        -Protocol $protocol `
        -PortNumber $port `
        -FtpSecure Explicit `
        -FtpMode Passive `
        @paramCertType `
        -SessionLogPath $WinScpLog | `
            Send-WinSCPItem `
                -Path $pgpFileList `
                -Destination $folder `
                -Remove

    Friday, July 22, 2016 12:37 PM