Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Use third party module without installing it

I am sure this would be possible but can't find anything useful. I have written a lan scanner script. To automate things as much as possible, I don't rely on any input from user. The script checks the local interface IP address and uses Indented Network Tools module to calculate the number of possible IP addresses and pings each one.

The problem is since I am using a third party tool, I have to install it on any machine that I want to use this script on. Is there a way I can include this third party module with my script like put them in the same folder and not have to install it separately?

like image 379
Junaid Avatar asked Sep 16 '16 01:09

Junaid


People also ask

How do I manually import a PowerShell Module?

Method 2—Install PowerShell Modules Manually To install PowerShell modules manually, you first need to determine your current PowerShell module directory path, download your new module to that path, and invoke the import-module command to let windows know it's there.

How do I import a PowerShell Module offline?

The first step for installing a powershell module on an offline computer is to download it with a computer that is connected to the internet. In the Start menu search for the Windows Powershell application and open it. On the command line type Save-Module -Name ModuleName -Path “FilePath” and Enter to run the command.

How do I run PowerShellGet?

Right-click the PowerShell icon and choose Run as administrator to start an elevated session. Run the following command to install the NuGet provider. After you have installed the provider you should be able to use any of the PowerShellGet cmdlets with the PowerShell Gallery.


1 Answers

That really depends on how you want to deploy your module to other machines. If you want to share it on a network share or distribute a zip package, then you can include these dependencies along with your module. Just put Indented.Common and Indented.NetworkTools in one directory with your script definition, like so:

MyModule/
└╴MyModule.psm1
└╴Indented.Common/
└╴IndentedNetworkTools/

Then, you can load these modules directly from MyModule.psm1 (without installing them to a global modules path):

import-module $psscriptroot\Indented.Common\Indented.Common.psm1
import-module $psscriptroot\Indented.NetworkTools\Indented.NetworkTools.psm1

And that's it. This will also work if you have a normal .ps1, not a .psm1 module.

Perhaps a more elegant way would be to use WMF5 PackageManagement. Declare Indented.NetworkTools as dependencies (NestedModules) in MyModule.psd1, then publish it on PSGallery. Then, you can just say Install-Module MyModule on other machines - this will install MyModule and it's dependencies.

The problem with this approach is that any declared dependencies have to be also available on PowershellGallery (which Indented.* modules are not).

like image 154
qbik Avatar answered Oct 12 '22 22:10

qbik