passing array of strings parameter to a script flattens it

Welcome Forums General PowerShell Q&A passing array of strings parameter to a script flattens it

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #92656

    Participant
    Points: 15
    Rank: Member

    Hi.
    I have a script1 that has an array of strings as one of the parameters:
    [Parameter(Mandatory=$True)]
    [string[]]$RDSL

    When script1 is called and I supply it 2 (or more) strings, it works fine, but we call script1 from another script2.

    In this script 2, it's defined/called like this:
    [Parameter(Mandatory=$False)]
    [string[]]$RDSL = @("server1","server2")
    ...
    Execute-Script -Command 'powershell.exe -NonInteractive -NoProfile -File "\\unc_path\Script1.ps1" -RDSL $RDSL
    When executed like this, the script gets executed, but it parses the array as simple string (it seems) 'server1 server2' and the command that expects the array fails.

    Any idea what am I doing wrong?

  • #92666

    Participant
    Points: 165
    Helping Hand
    Rank: Participant

    What is Execute-Script? What kind of script is this script2? Is this script2 able to "enumerate" Powershell arrays?

  • #93294

    Participant
    Points: 15
    Rank: Member

    Sorry, missed the reply.
    These are scripts that automate server deployment and configuration. There's one master script that has parameters that are parsed to individual scripts using the Execute-Script function that executes scripts and checks exit values (defined in the individual scripts).

    Function Execute-Script {
        Param(
        [Parameter(Mandatory=$True)]
        [string]$Command
        )
    
        $global:LASTEXITCODE = 10000
    
        #execute the command
        Write-Host -ForegroundColor Cyan "Running '$Command'"
        Invoke-Expression $Command
        if ($LASTEXITCODE -eq 0) {
            Write-Host -ForegroundColor Green "Command '$Command' SUCCESS"
            Write-Log -Message "Command '$Command' finished successfully" -Log $LogFile -Level INFO
        } else {
            write-host -ForegroundColor Red "Command '$Command' FAILED, check log"
            Write-Log -Message "Command '$Command' failed, exit code: $LASTEXITCODE" -Log $LogFile -Level ERROR
            Pause 
        }
    }

    master script (excerpt):

    ...
    [Parameter(Mandatory=$False)]
    [string[]]$RDSL = @("server1","server2"),
    ...
    Execute-Script -Command 'powershell.exe -NonInteractive -NoProfile -File "\\fs\Scripts\script2.ps1" -ComputerName $ComputerName -RDSL $RDSL -RDGW $RDGW -RDCert $RDCert -RDCertPwd $RDCertPwd'
    ...

    script2 (excerpt):

    ...
     [string[]]$RDSL,
        [Parameter(Mandatory=$True)]
    ...
    Set-RDLicenseConfiguration -ConnectionBroker $ComputerFQDN -LicenseServer $RDSL -Mode PerUser -Force
    #the above cmd then fails with the problem I described - from logging it seems it's flattened to string 'server1 server2'

    Is it more clear now?

  • #94782

    Participant
    Points: 15
    Rank: Member

    Anyone? Still trying to figure this one out.

    • #94819

      Participant
      Points: 5
      Rank: Member

      You could do something like this in your string for command.

      -RDSL @('$($rdsl -join ''',''')')
      

      Or something like that. If you just did the $RDSL -join ',' you would get the server1,server2 so it would work if there weren't spaces in the names.

    • #95317

      Participant
      Points: 15
      Rank: Member

      Hi. Unfortunately, this doesn't work either:
      Execute-Script : A positional parameter cannot be found that accepts argument 'server1',server2'.

      Any other ideas?

  • #95325

    Participant
    Points: 15
    Rank: Member

    I've fixed the script by defining the 'array' as simple string (separated by comma) in the master script and then converting it to an array in script2.

    master script:

    [Parameter(Mandatory=$False)]
    [string]$RDSL = 'server1,server2'
    ...
    Execute-Script -Command 'powershell.exe -NonInteractive -NoProfile -File "\\fs\Scripts\script2.ps1" -ComputerName $ComputerName -RDSL $RDSL -RDGW $RDGW -RDCert $RDCert -RDCertPwd $RDCertPwd'

    script2:

    ...
     [Parameter(Mandatory=$true)
     [string]$RDSL
    ...
    [string[]]$RDSLArr = $RDSL.Split(',')
    Set-RDLicenseConfiguration -ConnectionBroker $ComputerFQDN -LicenseServer $RDSLArr -Mode PerUser -Force
  • #105713

    Participant
    Points: -19
    Rank: Member

    If you want a parameter to accept an array of items, (like strings), you'll need to specify it:

    [string]$RDSL

    should be:

    [string[]]$RDSL

    Specifying the $RDSL variable as just a string (and not a string array) will give you the error message you have reported.

The topic ‘passing array of strings parameter to a script flattens it’ is closed to new replies.