Created
March 28, 2019 12:42
-
-
Save stephanlinke/05eb042123e185c13bce934c94970479 to your computer and use it in GitHub Desktop.
Simple Setup with logging.
This file contains hidden or 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
| #region modules | |
| $requiredModules = @("<modulname>") | |
| $numbers = @{ | |
| 2 = "two" | |
| 3 = "three" | |
| 4 = "four" | |
| 5 = "five" | |
| 6 = "six" | |
| 7 = "seven" | |
| 8 = "eight" | |
| 9 = "nine" | |
| } | |
| function This-ShowMessage([string]$type,$message){ | |
| Write-Host ("[{0}] " -f (Get-Date)) -NoNewline; | |
| switch ($type){ | |
| "success" { Write-Host " success " -BackgroundColor Green -ForegroundColor White -NoNewline; } | |
| "information" { Write-Host " information " -BackgroundColor DarkCyan -ForegroundColor White -NoNewline; } | |
| "warning" { Write-Host " warning " -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; } | |
| "error" { Write-Host " error " -BackgroundColor DarkRed -ForegroundColor White -NoNewline; } | |
| default { Write-Host " notes " -BackgroundColor DarkGray -ForegroundColor White -NoNewline; } | |
| } | |
| Write-Host (" {0}{1}" -f $message,$Global:blank) | |
| } | |
| function This-Setup { | |
| $installed = 0; | |
| Switch($requiredModules.Count){ | |
| {$_ -eq 1 } { This-ShowMessage -type information -message ([string]::Format("This script has one dependency: {0}", $requiredModules -join ", ")) } | |
| {$_ -gt 1 } { This-ShowMessage -type information -message ([string]::Format("This script has {0} dependencies: {1}", $requiredModules.Count, $requiredModules -join ", ")) } | |
| } | |
| foreach($requiredModule in $requiredModules){ | |
| # Check if our module loaded properly | |
| This-ShowMessage -type information -message "Checking for module $($requiredModule)" | |
| if (Get-Module -ListAvailable -Name $requiredModule) { | |
| try { Import-Module $requiredModule -Verbose:$false; This-ShowMessage -type success -message "$($requiredModule) loaded."; $installed++} | |
| catch { This-ShowMessage -type error -message "There was a problem loading $($requiredModule). Details of the error:`n$($_.Exception.Message)"} | |
| } | |
| else { | |
| This-ShowMessage -type information "$($requiredModule) not installed yet. Installing now..." | |
| try { Install-Module $requiredModule -Force -Verbose:$false; } | |
| catch { This-ShowMessage -type error -message "There was a problem installing $($requiredModule). Details of the error:`n$($_.Exception.Message)"} } | |
| } | |
| if($installed -eq $requiredModules.Count) | |
| { This-ShowMessage -type success -message "All dependencies loaded."} | |
| else | |
| { This-ShowMessage -type error -message "At least one dependency was not met. Exiting..."; Exit 0 } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment