【问题标题】:using module $PSScriptRoot: is not a valid value for using nameusing module $PSScriptRoot: is not a valid value for using name
【发布时间】:2019-09-19 09:17:05
【问题描述】:

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 电脑。

【问题讨论】:

标签: powershell powershell-module


【解决方案1】:

此答案基于 David 对该问题的评论。

“要在 Using 模块语句中使用变量值,请创建一个包含 Using 模块语句的脚本块并将其点源到脚本中。(感谢 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

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 2022-01-09
    • 2017-11-28
    • 2017-05-01
    • 2020-11-08
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-26
    相关资源
    最近更新 更多