2

I am trying to execute a PowerShell file from Windows CMD using below command.

powershell -NoProfile -NonInteractive -ExecutionPolicy ByPass -File C:\Users\akarri\Desktop\Temp\test1.ps1 -ConfigPathsArray @(1,2,4)

PowerShell code:

Param(
   [Parameter(Mandatory=$True,Position=0)]
   [Object[]] $ConfigPathsArray       
)

function Start-Transform($configsArray) {
    [Object[]] $test = @(1,2,4)
    $test.GetType()

    foreach ($configArray in $test) {
        Write-Output $configArray
    }

    $configsArray.GetType()
    foreach ($configArray in $configsArray) {
        Write-Output $configArray
    }
}

Start-Transform -configsArray $ConfigPathsArray -Verbose

Though I am passing an array from CMD, the count of $configPathsArray is giving me is 1.

It is working fine when I execute the command with -Command option instead of -File but I need your help in executing the same with -File command (Working with TeamCity Enterprise 2019.1.1 (build 66192))

0

1 Answer 1

1

about_powershell.exe:

In contrast, running powershell.exe -File .\test.ps1 -TestParam $env:windir in cmd.exe results in the script receiving the literal string $env:windir because it has no special meaning to the current cmd.exe shell.

This means that @(1,2,4) is passed as literal string to your string, because it won't be expanded to an array. As result $ConfigPathsArray holds one string entry @(1,2,4), therefore Count returns 1.

You could try to use the TeamCity PowerShell Runner as described here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.