PowerShell Requires -Modules: ModuleVersion "Argument must be constant"
I was looking to make use of a PowerShell feature which prevents a script from running without the required elements. Detailed here (about_Requires) there are examples for requiring specific versions of PowerShell, snap-ins and modules, and administrative rights.
In particular I was looking at the modules example given in the documentation:
#Requires -Modules PSWorkflow, @{ModuleName="PSScheduledJob";ModuleVersion=1.0.0.0}
Unfortunately, using this example as is given generates an error Argument must be constant:
C:\\Scripts\\Scratch> .\\Test-Requires.ps1 At C:\\Scripts\\Scratch\\Test-Requires.ps1:1 char:20 + #Requires -Modules PSWorkflow, @{ModuleName="PSScheduledJob";ModuleVersion=1.0.0 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Argument must be constant. + CategoryInfo : ParserError: (:) \[\], ParseException + FullyQualifiedErrorId : RequiresArgumentMustBeConstant
The correct syntax for the example should read:
#Requires -Modules PSWorkflow, @{ModuleName="PSScheduledJob";ModuleVersion="1.0.0.0"}
i.e. quotes around the ModuleVersion number. So in a contrived example where I bump the version number up to 1.2.0.0, running the script now gives me the response I am looking for:
I logged a documentation bug here.