2

我正在尝试编写 PowerShell,但失败得很惨。

Set-ExecutionPolicy Unrestricted
Import-Module -Assembly PowerShellXrm.Framework.CI.PowerShell.dll

Set-ExecutionPolicy Unrestricted 
Import-Module -Assembly "PowerShellXrm.Framework.CI.PowerShell.dll"

并得到以下错误。

Import-Module : Cannot bind parameter 'Assembly'. Cannot convert the
"PowerShellXrm.Framework.CI.PowerShell.dll" value of type "System.String"
to type "System.Reflection.Assembly".
At line:1 char:25
+ Import-Module -Assembly PowerShellXrm.Framework.CI.PowerShell.dll
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Import-Module], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportModuleCommand

PowerShell 脚本保存在与程序集相同的位置PowerShellXrm.Framework.CI.PowerShell.dll。我也尝试过包含组装的完整路径,但没有运气。

4

2 回答 2

2

如果要从 DLL 文件中导入 PowerShell 模块,只需传递文件名:

Import-Module 'PowerShellXrm.Framework.CI.PowerShell.dll'

如果文件不在下列文件夹之一中,请使用完整路径$env:PSModulePath

Import-Module 'C:\path\to\PowerShellXrm.Framework.CI.PowerShell.dll'

文件所述,该-Assembly参数用于导入程序集对象,而不是程序集文件

-组装<组装[]>

导入在指定程序集对象中实现的 cmdlet 和提供程序。输入包含装配对象的变量或创建装配对象的命令。您还可以通过管道将程序集对象传递给 Import-Module。

于 2015-12-10T09:09:58.483 回答
0

如果要使用-Assembly参数,可以使用以下内容:

$assembly = [System.Reflection.Assembly]::LoadFrom('PowerShellXrm.Framework.CI.PowerShell.dll')
Import-Module -Assembly $assembly
于 2019-04-25T12:20:46.460 回答