functions/function-Initialize-LHModule.ps1
function Initialize-LHModule { [CmdletBinding()] param ( [Parameter(Mandatory)] [string] $Name ) #Start logging Start-Transcript -Path "C:\Modern Workplace\$Name - $(((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmssZ")).log" -Force # Determine if the module needs to be installed try { Write-Host "Attempting to locate $Name module..." $installedModuleVersion = Get-InstalledModule -Name $Name -ErrorAction Stop -Verbose:$false if ($null -ne $installedModuleVersion) { Write-Host "FOUND! Checking for latest version..." $latestModuleVersion = (Find-Module -Name $Name -ErrorAction Stop -Verbose:$false).Version if ($latestModuleVersion -gt $installedModuleVersion.Version) { Write-Host "Latest version of $Name module is not installed [$($installedModuleVersion.ToString())], attempting to install: $($latestModuleVersion.ToString())" Update-Module -Name $Name -Scope CurrentUser -Force -ErrorAction Stop -Confirm:$false -Verbose:$false } else { Write-Host "Installed version is up-to-date [$($latestModuleVersion.ToString())]" } } } catch [System.Exception] { Write-Host "Unable to detect $Name module, attempting to install from PSGallery" try { # Install NuGet package provider Install-PackageProvider -Name NuGet -Force -Verbose:$false # Install module Install-Module -Name $Name -Scope AllUsers -Force -ErrorAction Stop -Confirm:$false -Verbose:$false Write-Host "Successfully installed $Name" } catch [System.Exception] { Write-Host "An error occurred while attempting to install $Name module. Error message: $($_.Exception.Message)" ; break } } # Stop Logging Stop-Transcript } |