Has any one on here successfully imported Powershell modules into Swimlane Powershell scripts. The module works from my prompt but it fails when running in Swimlane.
Yeah, this is something I’ve seen people run into a few times. Essentially you need to import the module persistently, for all PS sessions. Then you can use it inside Swimlane no problem.
I think the best way to answer this is with an example. Let’s walk through importing the New-SWRandomPassword method/module. (It’s a great method for generating random passwords. You can get it here https://gallery.technet.microsoft.com/scriptcenter/Generate-a-random-and-5c879ed5)
Go here https://gallery.technet.microsoft.com/scriptcenter/Generate-a-random-and-5c879ed5 and download New-SWRandomPassword.ps1
Next, on your Swimlane host itself, create a folder called “New-SWRandomPassword” in this directory: C:\Windows\System32\WindowsPowerShell\v1.0\Modules.
Copy the script you downloaded from step 1, into that folder
PowerShell modules must have the .psm1 extension. The one we downloaded didn’t. So rename the extension from “.ps1” to “.psm1”
Open an Administrative command prompt. Switch to PowerShell (by typing “powershell”), and run this command "Import-Module -name “New-SWRandomPassword” This will enable the function for all PS sessions on the host.
We can now use that new method inside Swimlane. You can easily test it from the Config tab
Actually, you aren’t importing the module “globally to all users”, Import-Module only loads the module into your current PS session, (two PowerShell instances won’t share modules imported, or variables, etc. as they are in separate sessions). I believe Swimlane will execute an integration (task) in it’s own isolated PS session, as that is typically best practice. The behavior you are most likely experiencing above is PS’s Auto Load-Module feature (introduced in v3.0) which it is able to resolve the command,
New-SWRandomPassword's PS Module as you installed it in the system-wide PS Module directory (
$env:PSModulePath). More information can be found at https://msdn.microsoft.com/en-us/library/dd878284(v=vs.85).aspx
If you want it to actually import modules globally (or by user), you would use the PS’s profile script (https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx). However I would recommend explicitly calling
Import-Module in your script, in my opinion. It documents the dependencies your script requires, instead of relying on the automagically import feature. While it is great when in an interactive PS session (powershell.exe/pwsh) it can cause lost knowledge of the scripts requirements (i.e. modules in use).
This is good info. Thanks Dan! (https://www.youtube.com/watch?v=GD6qtc2_AQA)