Swimlane Powershell Scripting

Issue
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)

  1. Go here https://gallery.technet.microsoft.com/scriptcenter/Generate-a-random-and-5c879ed5 and download New-SWRandomPassword.ps1

  2. Next, on your Swimlane host itself, create a folder called “New-SWRandomPassword” in this directory: C:\Windows\System32\WindowsPowerShell\v1.0\Modules.

    (not a bad idea to verify that path is in your PSModule Path by running $env:PSModulePath)image

  3. Copy the script you downloaded from step 1, into that folder

  4. PowerShell modules must have the .psm1 extension. The one we downloaded didn’t. So rename the extension from “.ps1” to “.psm1”

  5. Open an Administrative command prompt. Switch to PowerShell (by typing “powershell”), and run this command "Import-Module -name “New-SWRandomPassword” image This will enable the function for all PS sessions on the host.

  6. We can now use that new method inside Swimlane. You can easily test it from the Config tab

3 Likes

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).

3 Likes

This is good info. Thanks Dan! (https://www.youtube.com/watch?v=GD6qtc2_AQA)

1 Like

Thanks @mark.vankempen and @DanStory for the explanation. This worked out perfectly for me!!

1 Like