0

Afternoon on a friday, I am playing around with calling a powershell script from the cmd (similar to how Nuke does their call for "build") but I can't get an array parameter to correctly pass through and populate.

Here is the setup: I have 1 text file that is "masterswitch.cmd" and it is a one-liner that just calls the powershell script "masterswitch.ps1", all in the same directory.

powershell -ExecutionPolicy ByPass -NoProfile -File "%~dp0masterswitch.ps1" %*

The content of the "masterswitch.ps1" file is below.

[CmdletBinding()]
Param(
    [Alias("n")]
    [string]$meal,
    
    [Alias("e")]
    [array]$foods,
    
    [Alias("h")]
    [switch]$help
)

if ($Help){
    powershell -command "get-help $PSCommandPath -Detailed"
    exit
}
if ($meal.length -eq 0){
    Write-Output "`n No meal to eat"
    exit}
if ($foods.length -eq 0){
    Write-Output "`n No foods where provided"
    exit}
$i = 0
foreach ( $line in $foods) {
    write "[$i] $line"
    $i++
}

open up a cmd window and cd to the directory where those 2 files exist. Then run masterswitch -h works just fine. So does masterswitch -n lunch with the expected notification that -foods is missing.

But when I run masterswitch -n dinner -e burritos,nachos I get the output of [0] burritos,nachos.

What I should get, and what I get when I run it from the powershell ide, is:

[0] burritos
[1] nachos

So what in my setup of the one-liner "masterswitch.cmd" file is blocking the ability for powershell to properly parse my passed array? (yes, I realize I can turn it into a string and parse myself)

Update

Clarity to the answer below. All that had to happen was change that one liner from -File to -Command. So the new one liner is

powershell -ExecutionPolicy ByPass -NoProfile -Command "%~dp0masterswitch.ps1" %*

4
  • Try [string[]]$foods. Commented Sep 24, 2021 at 20:43
  • same result as using [array] Commented Sep 24, 2021 at 20:47
  • both [string[]] and [array] work correctly from the powershell ide but not when executing from the cmd call. Which is weird because it passes the other parameters just fine Commented Sep 24, 2021 at 20:48
  • when running from the normal command prompt or from the powershell ide? Commented Sep 24, 2021 at 20:57

1 Answer 1

1

Does this code produce the results you are seeking?

PS C:\src\t> type .\foods.ps1
[CmdletBinding()]
Param(
    [Alias("n")]
    [string]$meal,

    [Alias("e")]
    [string[]]$foods,

    [Alias("h")]
    [switch]$help
)

if ($Help){
    powershell -command "get-help $PSCommandPath -Detailed"
    exit
}
if ($meal.length -eq 0){
    Write-Output "`n No meal to eat"
    exit}
if ($foods.length -eq 0){
    Write-Output "`n No foods where provided"
    exit}
$i = 0
foreach ( $line in $foods) {
    write "[$i] $line"
    $i++
}
PS C:\src\t> .\foods.ps1 -n lunch -e apple,orange
[0] apple
[1] orange

Update:

16:00:45.10  C:\src\t
C:>powershell -NoLogo -NoProfile -Command ".\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange

Update 2:

16:01:17.45  C:\src\t
C:>powershell -NoLogo -NoProfile -Command "C:\src\t\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange
Sign up to request clarification or add additional context in comments.

4 Comments

yes, when running from a powershell environment it runs correctly. However if you run from a normal command prompt window, it does not
@AdrianHoffman, I added an example of running it from cmd.exe.
yep, that I could get to work also. So how would I reshape that into the one-liner cmd file so I can run foods -n lunch -e apple,orange from the command line and not explicitly set those parameters in the file? Did I word that in a way that made sense?
Run it as a -Command and not a -File.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.