passing array of strings parameter to a script flattens it

This topic contains 2 replies, has 2 voices, and was last updated by  John Mooper 1 week, 4 days ago.

  • Author
    Posts
  • #92656

    John Mooper
    Participant

    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

    Olaf Soyk
    Participant

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

  • #93294

    John Mooper
    Participant

    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?

You must be logged in to reply to this topic.