Defining Parameters

This topic contains 2 replies, has 2 voices, and was last updated by  Jeremiah Logan 1 week, 6 days ago.

  • Author
    Posts
  • #81395

    Jeremiah Logan
    Participant

    I am working on a script for which I have written a GUI. The user fills out the fields and then the GUI passes those by way of parameters to the underlying script. Below is a reproducer of what I am trying:

    Param (
       [string]$sVM,
       [string]$iCPU,
       [string]$iRAM,
       [string]$sIP,
       [string]$sSubnet,
       [string]$sGateway,
       [string]$sDNS,
       [string]$sDescription,
       [string]$sSite,
       [string]$sHost,
       [string]$sDStore,
       [string]$sTemplate,
       [string]$sSpec,
       [string]$sFolder,
       [string]$sOU
    )
    
    Write-Host $args.Count "args"
    
        ForEach ($sArg in $args) {
            Write-Host $sArg
        }
    

    I am relatively new to PowerShell, having come from other languages, but was under the impression the parameters had to be at the top of the script, which is why I formatted this way. When I run the script with something like this (quotes around those fields with spaces):

    Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com"

    The issue I am having is, if I run the script just as written above, I get the following output which seems to indicate it found none of the parameters:

    ./Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com"
    0 args

    If I add something to the first line, even just a simple Write-Host "Beginning" or a cls, I get an error on Param (which I would expect, it not being the first line), but the output is what I would expect:

    Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
    the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At desktop\Args.ps1:3 char:1
    + Param (
    + ~~~~~
    + CategoryInfo : ObjectNotFound: (Param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    15 args
    SPERA-SCCM01
    4
    16
    10.10.120.115
    255.255.255.0
    10.10.115.254
    10.10.1.120
    SCCM 2016 Server
    ERA
    ER-vms33.mycompany.com
    Dev-ERA-App01
    ER 2016 Template 02
    ScriptERADomain
    ScriptDeploy
    OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com

    I have to believe whatever I am doing wrong is simple stupid, but if someone could clue me in that would be much appreciated.

  • #81401

    Richard Siddaway
    Moderator

    When you define parameters the $args automatic variable takes the OVERFLOW

    Using this simple script

    param (
      [string]$param1,
      [string]$param2
    )
    
    Write-Host "`$param1 = $param1"
    Write-Host "`$param2 = $param2"
    Write-Host "`$args = $args"
    

    Test it like this

    PS> .\paramtest.ps1 "one"
    $param1 = one
    $param2 =
    $args =
    PS> .\paramtest.ps1 "one" "two"
    $param1 = one
    $param2 = two
    $args =
    PS> .\paramtest.ps1 "one" "two" "three"
    $param1 = one
    $param2 = two
    $args = three
    PS> .\paramtest.ps1 "one" "two" "three" "four"
    $param1 = one
    $param2 = two
    $args = three four

    You'll see that the values are given to the defined parameters first and only after they have been filled does $args get the leftovers.

    • #81403

      Jeremiah Logan
      Participant

      Lightbulb moment, I was unaware of that. I guess I thought $args took the full list of params you input. Thanks for this.

You must be logged in to reply to this topic.