Public/Invoke-TunerPSModuleCheck.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#requires -RunAsAdministrator
function Invoke-TunerPSModuleCheck {
    <#
    .SYNOPSIS
        Check PowerShell module installed versions
    .DESCRIPTION
        Check each installed PowerShell module against the latest available version
    .PARAMETER UpdateAll
        If version is older than on remote repository, install latest version
    .EXAMPLE
        Invoke-TunerPSModuleCheck
    .EXAMPLE
        Invoke-TunerPSModuleCheck -UpdateAll
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory=$False, HelpMessage="Force update of outdated modules")]
        [switch] $UpdateAll
    )
    try {
        $Modules = Get-Module -ListAvailable | Sort-Object Name
        $Modules | Foreach-Object {
            $mdv = $_.Version -join '.'
            if ($rpm = Find-Module $_.Name -ErrorAction SilentlyContinue) {
                $rpv = $rpm.Version -join '.'
                if ($mdv -ne $rpv) {
                    if ($UpdateAll) {
                        Write-Host "updating module: $($_.Name) $mdv to $rpv" -ForegroundColor Magenta
                        Uninstall-Module -Name $_ -AllVersions -Force
                        Install-Module -Name $_ -AllowClobber -Force
                    }
                    else {
                        Write-Host "module is out of date: $($_.Name) $mdv :: Latest is $rpv" -ForegroundColor Yellow
                    }
                }
                else {
                    Write-Host "module is current: $($_.Name)" -ForegroundColor Green
                }
            }
            else {
                Write-Warning "module not found in repository: $($_.Name)"
            }
        }
    }
    catch {
        Write-Error $Error[0].Exception.Message
    }
}

Export-ModuleMember -Function Invoke-TunerPSModuleCheck