When I run a simple script with #Requires -Version 4 -Modules TestModule
statement at the first line, I get an error:
The script 'TestScript.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: TestModule CategoryInfo : ResourceUnavailable: (TestScript.ps1:String) [], ScriptRequiresException FullyQualifiedErrorId : ScriptRequiresMissingModules
Some interesting facts:
#Requires -Version 4 -Modules TestModule
statement, but when I run a script with #Requires
pointing to that module, both modules are loaded without any problems.PSModulePath
.Get-Module -ListAvailable
does list TestModule and all its' commands.Import-Module
works the first time.$PSVersionTable
:
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18444
BuildVersion 6.3.9600.16406
I have no idea what's going on.
Found the reason.
I replaced #Requires
with Import-Module TestModule -Verbose
and found out that one of the assemblies could not be loaded, generating an error, but due to $ErrorActionPreference=Continue
the rest of the module loaded. It appears that #Requires
behaves differently than Import-Module
.