Install a PowerShell module and use it without restarting PowerShell session

Viewed 637

I am trying to find a way to install AWS Tools for PowerShell 4.1, and then immediately use the included cmdlets for interaction with S3. I have tried installing the MSI, but it only works after starting a new PS session. And I have tried using Install-Module -Name AWS.Tools.Common but that tells me I need an updated version of the Package Provider and to use Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force. But that fails with

WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''. WARNING: Unable to download the list of available providers. Check your internet connection.

The internet connection is fine.

My utility is a script that will be run on client machines, often Work From Home machines by non technical staff, to enable a content management system that uses AWS S3 to mirror Autodesk Revit content libraries. In short, I need something that works, works every time, and doesn't force staff to act like IT folks. So, is there a bullet proof way to do this, or is this getting so far beyond the intended use case for PowerShell that I need to find another solution? I would rather skip the NuGet, which seems VERY nerd IT focused, and just supply an MSI, but if NuGet can be made to work 100% of the time AND make the package available without rerunning the script, that would be OK.

1 Answers

As I (now) understand it, NuGet is really more for developers working in Visual Studio, not for PowerShell based client side tools that need to install modules as their own dependencies. SO, not really a solution for my needs. That said, I found the secret sauce for using an MSI is to add the module path manually after the install is complete, but to do it as a temporary path. So...

$installerPath = 'PATH TO... \AWSToolsAndSDKForNet_sdk-3.5.2.0_ps-4.1.0.0_tk-1.14.5.0.msi'
$awsModulePath = 'C:\Program Files (x86)\AWS Tools\PowerShell\'
$installProcess = Start-Process msiexec.exe -ArgumentList "/I $installerPath /quiet" -PassThru
$installProcess.WaitForExit()
$env:PSModulePath = $env:PSModulePath + "$([System.IO.Path]::PathSeparator)$awsModulePath"

This will run the installer, which adds the AWS path to the module path for subsequent sessions as part of the install. But the temporary module path addition allows for use of the module immediately in this session, without doubling up the AWS part of the path. A conditional based on the return code from the install, to add the module path and continue with whatever code needs the module only on successful install, is the next improvement.

Related