just getting started writing scripts

This topic contains 1 reply, has 2 voices, and was last updated by  Curtis Smith 1 month ago.

  • Author
    Posts
  • #99490

    John L.
    Participant

    I'm just getting started, and trying to pass command-line parameters to a PowerShell script.

    After checking the documentation, I thought something like this would work:

    Param ( String[] $PackageId, String[] $NOGDOC )

    However, after attempting it, the error messages seem to suggest that this method is for passing parameters to internal functions, rather than from the (external) command line.

    Are the command-line arguments available in a pre-defined variable?

    Thanks in advance for any help you can provide.

  • #99492

    Curtis Smith
    Participant

    It's a syntax issue.

    Example: (test.ps1)

    Param(
        [string] $Param1,
        [int] $Param2,
        [string[]] $Param3
    )
    
    Write-Host "$Param1 : $Param2"
    Write-Host "Array count: $($Param3.count)"
    $Param3 | ForEach-Object {Write-Host "ArrayValue: $_"}

    Usage:

    .\test.ps1 -Param1 "A String" -Param2 7 -Param3 "Value1", "Value2", "Value3"

    Results

    A String : 7
    Array count: 3
    ArrayValue: Value1
    ArrayValue: Value2
    ArrayValue: Value3

You must be logged in to reply to this topic.