Created
January 3, 2017 03:55
-
-
Save bender-the-greatest/57e60efbd92144df52f370ef801ad7db to your computer and use it in GitHub Desktop.
Installs modules if they are missing prior to import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Installs modules if they are missing prior to importing | |
# Does not upgrade modules, only installs them if they are missing | |
# | |
# Example usage: | |
# install-and-import pscx | |
function install-and-import { | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$moduleName | |
) | |
$oldErrorActionPreference = $ErrorActionPreference | |
$ErrorActionPreference = "Stop" | |
echo "Importing $moduleName" | |
if ( -not $( Get-Module -Name $moduleName -ListAvailable ) ) { | |
echo " Module $moduleName is not installed. Installing now..." | |
Install-Module -Force -AllowClobber $moduleName | |
} | |
Import-Module $moduleName | |
$ErrorActionPreference = $oldErrorActionPreference | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment