Created
October 4, 2016 17:50
-
-
Save aetos382/5b347dc6b3337f1e4e7e575a0fd60f60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Uninstall-OldModule { | |
[CmdletBinding( | |
SupportsShouldProcess, | |
ConfirmImpact = 'High')] | |
param( | |
[string] $Name, | |
[switch] $Force) | |
$params = @{} | |
if ($PSBoundParameters.ContainsKey('Name')) { | |
$params['Name'] = $Name | |
} | |
$uninstallParams = @{} | |
if ($PSBoundParameters.ContainsKey('WhatIf')) { | |
$uninstallParams['WhatIf'] = $PSBoundParameters['WhatIf'] | |
} | |
if ($PSBoundParameters.ContainsKey('Verbose')) { | |
$uninstallParams['Verbose'] = $PSBoundParameters['Verbose'] | |
} | |
$modules = Get-InstalledModule @params | |
$yesToAll = $false | |
$noToAll = $false | |
foreach ($name in $modules.Name) { | |
$modules2 = @(Get-InstalledModule -Name $name -AllVersions | Sort-Object -Property 'Version' -Descending) | |
if ($modules2.Count -eq 1) { | |
continue | |
} | |
$latestVersion = $modules2[0].Version | |
$uninstallVersions = @(($modules2 | Select-Object -Skip 1).Version) | |
if ($uninstallVersions.Count -eq 1) { | |
$versionString = $uninstallVersions[0] | |
} | |
else { | |
$versionString = "$($uninstallVersions[-1]) - $($uninstallVersions[0])" | |
} | |
$message = "モジュール $name の $versionString をアンインストールしますか?(最新バージョンは $latestVersion)" | |
if ($Force -or $PSCmdlet.ShouldContinue($message, 'モジュールのアンインストール', [ref] $yesToAll, [ref] $noToAll)) { | |
for ($i = 0; $i -lt $uninstallVersions.Count; ++$i) { | |
$v = $uninstallVersions[$i] | |
$status = "モジュール $name をアンインストールしています" | |
$operation = "バージョン $v をアンインストール中" | |
$percentComplete = ([double]$i / $uninstallVersions.Count) * 100 | |
Write-Progress -Activity 'モジュールのアンインストール' -Status $status -CurrentOperation $operation -PercentComplete $percentComplete | |
Uninstall-Module -Name $name -RequiredVersion $v @uninstallParams -Force | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment