mardi 19 mai 2020

How to write if exist statement block to check if Powershell Modules are already installed using array of names

I am trying to verify if Powershell Modules are already installed on the local computer.

If they are Write-Host $_ Exists

If they are not Install-Module -Name $_

I am getting very lost inside this script block. I am trying to use a pre-defined list of Modules Names stored in an array to check against the output of (Get-Module -ListAvailable *).Name | Get-Unique )

I have read the error output comments but am not progressing.

My current script is below... any help would be greatly appreciated.

Function imods {
Write-Host; CLS

$AddModulesArray = @(`
"Convert",`
"Get-UserProfile",`
"Get-WindowsVersion",`
"PoshInternals",`
"PSConsoleTheme",`
"PSSpeedTest",`
"UpdateOS",`
"WindowsPSModulePath",`
"WindowsConsoleFonts",`
"xFailOverCluster"`
)

ForEach ($item in $AddModulesArray)Get-Module -ListAvailable *.Name | Get-Unique ) {
If ($item -eq $_) {
  Write-Host
  Write-Host $_ Already Exists
  Read-Host -Prompt "Press Enter to continue"
  }
Else {
    Write-Host
    Write-Host "Installing $item"
    Install-Module $item
    Start-Sleep -s 3; {Continue}; CLS
    }
   }
  }

Additionally, I know that it is not considered good practice to break up lines using the ` but I find it easier to read this way.

Aucun commentaire:

Enregistrer un commentaire