Trying to create a function that checks if you have a module installed along with that module being a minimum version. I have gotten it to work as a stand-alone check but when I try to make it function so that I don't have to have it repeated over and over when I have to check for multiple modules it fails the check for second input the version.
#Check if Module is avaiable
Function Module-Check {
#Set Pramatators
Param(
[Parameter(
Mandatory=$true,
Position=0,
HelpMessage="Enter the Modules name has show in Powershell",
ParameterSetName="Module Name")]
[string]
$ModuleName,
[Parameter(
Mandatory=$true,
Position=1,
HelpMessage="Enter the Modules Major Version Required has show in Powershell",
ParameterSetName="Major Version")]
[string]
$ModuleVersion
)
$ModuleAvaiable = Get-Module -ListAvailable | Select Name,Version | Where-Object {$_.name -eq "$ModuleName"}
if ($ModuleAvaiable.Name.Count -eq '0') {
Write-Host -ForegroundColor Red "$ModuleName Module is not available on this computer"
$InstallModule=Read-Host "Would you like to install it now (Y)es or (N)o"
Switch -Wildcard($InstallModule)
{
"Y*" {$Action= Write-host "Importing $ModuleName Module"
Install-Module -Name "$ModuleName"}
"N*" {$Action= Write-host -ForegroundColor Yellow "Functions in the Powershell rely on This module and will not be able to function PS will now exit"
break
}
Default {$Action= Write-Host "No input selected ending script"
break}
}
return $Action
}
else {
Write-Host "$ModuleName" $ModuleAvaiable.Version"is Avaiable on this computer"
$RequiredVersion = ($ModuleAvaiable.Version.Major -ge $ModuleVersion)
if ($RequiredVersion = $true) {
}
else {
Write-Host -ForegroundColor Yellow "$ModuleName Module Version installed does not meet the required mimium version (version $ModuleVersion.X)"
$UpdateModule=Read-Host "Update the Module version now (Y)es or (N)o"
switch -Wildcard ($UpdateModule)
{
"Y*" {$Action2= Write-host "Update $ModuleName Module"
update-Module -Name "$ModuleName"}
"N*" {$Action2= Write-host -ForegroundColor Yellow "Functions in the Powershell rely on this module and may not function properly with a lower version installed"
}
Default {$Action2= Write-Host "No input selected updating ALL Modules"
foreach ($module in Get-InstalledModule) {
Get-InstalledModule $module.Name -AllVersions |
Where-Object Version -ne $module.Version |
Uninstall-Module
}
}
}
Return $Action2
}
}
}