1
votes

I have the script that works perfectly when run from ISE or VS Code with parameters given in code like

 param(
      [string]$Param1 = "example",
      [string[]]$Param2 = @('arrayparam1','arrayparam2'),
      [string[]]$Param3 = @('arrayparam1','arrayparam2'),
      [switch]Param4 = $true
    )

But whenever I run this script without params in code but with params from console like

.\Script -Param1 example -Param2 'arrayparam1','arrayparam2' -Param3 'arrayparam1','arrayparam2' -Param4

it starts running but it generates empty csv file (with param in code this works perfectly). So I was running this script with "powershell.exe -File" (which generated nice file) like this:

powershell.exe -File .\Script -Param1 example -Param2 'arrayparam1','arrayparam2' -Param3 'arrayparam1','arrayparam2' -Param4

But the problem is that it takes only first item from array. If second array is empty it gives second item from first array to second array as first item (this blew my mind).

My goal is to run this script with all parameters from console without "-powershell.exe -File". I have tried various ExecutionPolicy and now I'm using RemoteSigned. I am using powershell, powershell ISE and VS code to test this but it always works the same. I have tried passing arrays as "1","2", ('1','2'), @("1","2") but it never works.

Please help

1
This answer explains why you cannot pass arrays via powershell -File. - mklement0
As an aside: avoid defining [switch] parameters that default to $true; don't use a default value, which means the expected $false default value is used. - mklement0
As for why direct invocation of .\Script doesn't work: there's no obvious reason why it shouldn't, so you'll need to update your question with more information, ideally providing an MCVE (Minimal, Complete, and Verifiable Example). - mklement0
Also, [switch]Param4 = $true --> [switch]$Param4 (note the $) - Theo

1 Answers

0
votes

Thanks @mklement0, you actually helped because I have started to look more deeply into my code and I discovered that for some reason passing an array of pscustomobject to some other function is not working without "powershell.exe -File" so making it a global variable solved the problem and script now runs well with just ".\Filename.psi -param ex -param2 ex2, etc". Thanks everyone for the help :D