使用模块 $PSScriptRoot: 不是使用名称的有效值

using module $PSScriptRoot: is not a valid value for using name

如何将 using module$PSScriptRoot 一起使用?

using module $PSScriptRoot/../myfolder/base.psm1
# or: using module "$PSScriptRoot/../myfolder/base.psm1"

如果我这样做,我会得到这个错误:

using module $PSScriptRoot: is not a valid value for using name

感谢@DavidBrabant,我尝试了以下方法:

$scriptBody = "using module /Users/name/Development/tools/powershell/base/base.psm1"
$script = [ScriptBlock]::Create($scriptBody)
. $script

Class Go : MyBaseClass {
    ...

不幸的是我得到:

Unable to find type [MyBaseClass].PowerShell
Ignoring 'TypeNotFound' parse error on type 'MyBaseClass'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types 

感谢 @MathiasR.Jessen 我尝试使用“\”得到了相同的结果。如果有任何不同,我应该说我在 mac 计算机上。

此答案基于大卫对问题的评论。

“要在 Using 模块语句中使用变量值,请创建一个包含 Using 模块语句的脚本块,并将其 dot-source 放入脚本中。(感谢 Bartek Bielawski 提供此解决方案。)”

Param (
    [parameter(Mandatory)]
    [string]
    $ModuleName
)

$scriptBody = "using module $ModuleName"
$script = [ScriptBlock]::Create($scriptBody)
. $script

...

来源:https://info.sapien.com/index.php/scripting/scripting-classes/import-powershell-classes-from-modules