powershellcommand-line-arguments

Powershell string array argument not working as intended when -File is used


I'm using a Powershell script that I found here: https://github.com/Simbiat/SnoozeGuard

The works by passing a string array and intended, when run in the usual way:

> C:\fullPathToSnoozeGuard.ps1 -requiresSystem 'arg1', 'arg2', 'arg3'
Searching for "arg1"...
Searching for "arg2"...
Searching for "arg3"...
No processes found
Feeling sleepy

But when run in using -File, it seems to not take the arguments as an array:

> C:\fullPathToPowershell.exe -File C:\fullPathToSnoozeGuard.ps1 -requiresSystem 'arg1', 'arg2', 'arg3'

C:\fullPathToSnoozeGuard.ps1 : Cannot process argument transformation on parameter 'pollingRate'. Cannot
convert value "arg3" to type "System.Int32". Error: "Input string was not in a correct format."
    + CategoryInfo          : InvalidData: (:) [SnoozeGuard.ps1], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,SnoozeGuard.ps1

Why does the -File cause it to stop treating the arguments as an array? What do I need to change in regards to my command?


Solution

  • C:\fullPathToPowershell.exe -Command "C:\fullPathToSnoozeGuard.ps1 -requiresSystem 'arg1', 'arg2', 'arg3'"
    

    Additional information:


    [1] The same applies to pwsh, the PowerShell (Core) 7 CLI.
    However, note that these two CLIs differ with respect to whether -File or -Command is the default parameter in the absence of either: powershell.exe defaults to -Command, whereas pwsh \ pwsh.exe defaults to -File.