2

I am trying to write PowerShell and am failing miserably.

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

and

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

and get the following error.

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

The PowerShell script is saved in the same location as the PowerShellXrm.Framework.CI.PowerShell.dll assembly. I have also tried including the full path to the assembly with no luck.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user3845056
  • 489
  • 7
  • 25
  • 2
    `-Assembly` parameter want `Assembly` object not path. – user4003407 Dec 10 '15 at 06:46
  • Possible duplicate of [Run my third-party DLL file with PowerShell](http://stackoverflow.com/questions/7972141/run-my-third-party-dll-file-with-powershell) – vonPryz Dec 10 '15 at 06:51

2 Answers2

2

If you want to import a PowerShell module from a DLL file simply pass the filename:

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

Use the full path if the file is not located in one of the folders listed in $env:PSModulePath:

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

As documented the -Assembly parameter is for importing assembly objects, not assembly files.

-Assembly<Assembly[]>

Imports the cmdlets and providers implemented in the specified assembly objects. Enter a variable that contains assembly objects or a command that creates assembly objects. You can also pipe an assembly object to Import-Module.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

If you want to use -Assembly parameter you can use the following:

$assembly = [System.Reflection.Assembly]::LoadFrom('PowerShellXrm.Framework.CI.PowerShell.dll')
Import-Module -Assembly $assembly
Kirill Kovalenko
  • 2,121
  • 16
  • 18