Cataster
Cataster

Reputation: 3551

How to import Package (not module)?

I installed the SharpDx package from Here but is there a way to import it, just like import-module, so that I wouldnt have to specify the assembly (dll) path(s)?

For now, the only way I found is to use Add-Type to reference the assembly but is there no other way besides this?

#Reference where you kept the dxSharp files
$dxSharpPath = 'Path\To\DLLs'

#Load the sharpDX Libraries
Add-Type -Path "$dxSharpPath\sharpdx.dll"
Add-Type -Path "$dxSharpPath\Sharpdx.Directinput.dll"

#Create new DirectInput object
$dInput = New-Object -TypeName SharpDX.DirectInput.DirectInput

I am ultimately trying to create the directinput object

Upvotes: 1

Views: 810

Answers (1)

postanote
postanote

Reputation: 16116

You must tell PowerShell where the DLL is, no different than you'd have to if you loaded a module (.psm1 file with or without a manifest) of which you did not install to one of the defined PowerShell module paths.

Instead of Add-Type, you can also use reflection:

$customDLL = 'UncToYourDLL'
[Reflection.Assembly]::LoadFile($customDll)

See also Lee Holmes article on the topic here:

Load a Custom DLL from PowerShell

Update

Import-Module SomeNewCustomOr3rdP.dll

Import-Module : The specified module 'SomeNewCustomOr3rdP.dll' was not loaded because no valid module file was found in any module directory.

Of course, that error is pretty specific. It has no idea where to find it, because that name does not match a module name.

So, this ...

Import-Module 'c:\users\mj\desktop\SomeNewCustomOr3rdP.dll'

Or create a folder of the same basename as the DLL in the PSModulePath, copy the DLL to the that named folder and use import as normal

C:\Users\<username>\Documents\WindowsPowerShell\Modules\SomeNewCustomOr3rdP\SomeNewCustomOr3rdP.dll'

Then this...

Import-Module SomeNewCustomOr3rdP

... should work as expected. All-in-all, Add-Type, Import-Module, and [Reflection.Assembly]::LoadFile($customDll), all accomplish the same thing.

Update regarding getting SharpDX as a module

Note that this is Install-Module and InstallPackage cmdlets. Both to the same thing for the target resource. If it exists via either, then the approach is the same for getting/using them.

# Find all modules wiht share in the name
Find-Module -Name '*Sharp*' | 
Format-Table -AutoSize
<#
# Results

Version        Name                             Repository Description                                                                                              
-------        ----                             ---------- -----------                                                                                              
0.9.1.326      ACMESharp                        PSGallery  Client library for the ACME protocol, which is used to interoperate with the Let's Encrypt project's C...
0.9.3.334      ACMESharp.Providers.IIS          PSGallery  Microsoft IIS Provider extension library for ACMESharp Client.                                           
0.9.1.326      ACMESharp.Providers.AWS          PSGallery  AWS Provider extension library for ACMESharp Client.                                                     
0.9.1.326      ACMESharp.Providers.Windows      PSGallery  Microsoft Windows Providers extension library for ACMESharp Client.                                      
0.0.1          SNMPSharpNet                     PSGallery  PowerShell module implementing SNMP cmdlets from the SNMP Sharp .NET Library                             
0.9.1.326      ACMESharp.Providers.CloudFlare   PSGallery  CloudFlare Provider extension library for ACMESharp Client.                                              
1.0.1          CowsaySharp                      PSGallery  Generates ASCII pictures of a cow with a message                                                         
1.0.4          ACMESharpRoute53Automation       PSGallery  ACMESharpRoute53Automation is a PowerShell module which automates the ACMESharp process of obtaining S...
0.9.1.326      ACMESharp.Providers.DNSMadeEasy  PSGallery  DNS Made Easy Provider extension library for ACMESharp Client.                                           
1.0.6563.38109 ACMESharp.Providers.QCloud       PSGallery  A QCloud dns provider for handling Challenges.                                                           
0.1.0          PSCSharpInvoker                  PSGallery  Adds a cmdlet that can be used to invoke C# code without loading the types in the current PowerShell n...
1.0.0.0        IntelliTect.ResharperNugetSearch PSGallery  Provides functions for searching against Jet Brains' Resharper Nuget Search API.                         
1.3.5          CSharp-Watch                     PSGallery  Watches the current directory and sub-directories for changes to C-Sharp files. When a change is detec...
#>

# find all packages with sharp in the name
Find-Package -Name '*Sharp*' | 
Format-Table -AutoSize

# Get the detail on SharpDx specifically
Find-Package -Name 'SharpDX'
<#
# Results

Name                           Version          Source           Summary                                                                                            
----                           -------          ------           -------                                                                                            
SharpDX                        4.2.0            nuget.org        Core assembly for all SharpDX assemblies.  
#>
Find-Package -Name 'SharpDX' | 
Format-List

# Download and save a module or package
Find-Package -Name 'SharpDX' | 
Save-Package -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
Install-Package -Name 'SharpDX' -Force

Upvotes: 3

Related Questions