1

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Ekrad
  • 21
  • 4
  • [This answer](https://stackoverflow.com/a/48959328/45375) explains why you cannot pass _arrays_ via `powershell -File`. – mklement0 Feb 19 '20 at 17:49
  • 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 Feb 19 '20 at 17:50
  • 1
    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)](http://stackoverflow.com/help/mcve). – mklement0 Feb 19 '20 at 17:52
  • 1
    Also, `[switch]Param4 = $true` --> `[switch]$Param4` (note the `$`) – Theo Feb 19 '20 at 19:11

1 Answers1

0

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

Ekrad
  • 21
  • 4