Public/Install-Dependencies.ps1

Function Install-Dependencies {
    <#
.SYNOPSIS
    This Function Installs, Updates, and troubleshoots all Other modules that this tool relies on
.NOTES
    Name: Install-Dependencies
    Author: Luke Hagar
    Version: 1.0
    DateCreated: 5/12/2021
.EXAMPLE
    Install-Dependencies
 
.EXAMPLE
    Install-Dependencies -Dependencies ImportExcel, psPAS
 
#>

    [CmdletBinding()]
    param (
        [Parameter()]
        $Dependencies = @("ImportExcel", "psPAS", "ADSIPS")
    )
    Foreach ($Module in $Dependencies) {
        Write-Verbose "Checking $Module"
        $Current = $null
        $Installed = $null
        Try {
            $Current = Find-Module $Module
            $Installed = Get-InstalledModule -AllVersions $Module
        
            If ($null -eq $Installed) {
                Write-Verbose "$Module Module is not installed, Installing Now"
                Install-Module $Module -Force
            }
            ElseIf ($null -ne $Installed) {
                if ($Installed.Version -notcontains $Current.Version) {
                    Write-Verbose "$Module Module is not current, Updating Now"
                    Update-Module $Module -Force
                }
                else {
                    Write-Verbose "$Module Module is already Installed and Current"
                }
            }
        }
        Catch {
            Write-Warning $_
            Write-Verbose "Errors Detected, Forcing Uninstall and Reinstall"
            $Installed | Uninstall-Module -Force
            Install-Module $Module -Force
        }
    }
    $FinalInstalled = Get-Installedmodule $Dependencies
    $FinalResults = @()
    Foreach ($Entry in $FinalInstalled) {
        $Temp = Find-Module $Entry.Name
        $FinalResults += [PSCustomObject]@{
            CurrentVersion   = $Temp.Version
            InstalledVersion = $Entry.Version
            Name             = $Entry.Name
            Repository       = $Entry.Repository
        }
    }
    Return $FinalResults
}