閱讀英文

共用方式為


卸載 Azure PowerShell 模組

本文說明如何卸載舊版的 Azure PowerShell,或從您的系統完全移除它。 如果您已決定完全卸載 Azure PowerShell,請透過 Send-Feedback Cmdlet 提供一些意見反應。 如果您遇到錯誤,當您 提出 GitHub 問題 以便修正此問題時,我們很感激。

從 MSI 卸載 Az PowerShell 模組

如果您使用 MSI 套件安裝 Az PowerShell 模組,則必須透過 Windows 系統而非 PowerShell 卸載。

平台 Instructions
Windows 10 啟動 > 設定 > 應用程式
Windows 7
Windows 8
[開始] > [控制台] > [程式集] > [解除安裝程式]

在此畫面上,您應該會在程式清單中看到 Azure PowerShell 。 這是要卸載的應用程式。 如果您沒有看到此程式列出,則您透過 PowerShellGet 安裝,並應遵循下一組指示。

從 PowerShellGet 卸載 Az PowerShell 模組

若要卸載 Az 模組,您可以使用 Uninstall-Module Cmdlet。 不過, Uninstall-Module 只會卸載一個模組。 若要完全移除 Az PowerShell 模組,您必須個別卸載每個模組。 如果您已安裝一個以上的 Azure PowerShell 版本,卸載可能會很複雜。

若要檢查您已安裝的 Az PowerShell 模組版本,請執行下列命令:

PowerShell
Get-InstalledModule -Name Az -AllVersions
輸出
Version             Name                           Repository           Description
-------             ----                           ----------           -----------
3.8.0               Az                             PSGallery            Microsoft Azure PowerShell
4.1.0               Az                             PSGallery            Microsoft Azure PowerShell

下列腳本會查詢 PowerShell 資源庫以取得相依子模組的清單。 然後,腳本會卸載每個子模組的正確版本。 您必須有系統管理員存取權,才能在進程 目前使用者 以外的 範圍內執行此腳本。

Azure PowerShell
function Uninstall-AzModule {
  [CmdletBinding(SupportsShouldProcess)]
  param(
    [ValidateNotNullOrEmpty()]
    [ValidateSet('Az','AzureRM','Azure')]
    [string]$Name = 'Az',

    [Parameter(Mandatory)]
    [string]$Version,

    [switch]$AllowPrerelease
  )

  $Params = @{}

  if ($PSBoundParameters.AllowPrerelease) {
    $Params.AllowPrerelease = $true
  }

  $IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')

  if (-not(Get-InstalledModule -Name $Name -RequiredVersion $Version -ErrorAction SilentlyContinue -OutVariable RootModule @Params)) {

    Write-Warning -Message "Uninstall aborted. $Name version $Version not found."

  } elseif (($RootModule.InstalledLocation -notlike "*$env:USERPROFILE*") -and ($IsAdmin -eq $false)) {

    Write-Warning -Message "Uninstall aborted. $Name version $Version exists in a system path. PowerShell must be run elevated as an admin to remove it."

  } elseif ((Get-Process -Name powershell, pwsh -OutVariable Sessions -ErrorAction SilentlyContinue).Count -gt 1) {

    Write-Warning -Message "Uninstall aborted. Please close all other PowerShell sessions before continuing. There are currently $($Sessions.Count) PowerShell sessions running."

  } else {
    Write-Verbose -Message 'Creating list of dependencies...'
    $target = Find-Module -Name $Name -RequiredVersion $Version @Params

    $AllModules = @([pscustomobject]@{
      Name = $Name
      Version = $Version
    })

    $AllModules += foreach ($dependency in $target.Dependencies) {
      switch ($dependency.keys) {
        {$_ -contains 'RequiredVersion'} {$UninstallVersion = $dependency.RequiredVersion; break}
        {$_ -contains 'MinimumVersion'} {$UninstallVersion = $dependency.MinimumVersion; break}
      }

      [pscustomobject]@{
        Name = $dependency.Name
        Version = $UninstallVersion
      }
    }

    [int]$i = 100 / $AllModules.Count

    foreach ($module in $AllModules) {
      Write-Progress -Activity 'Uninstallation in Progress' -Status "$i% Complete:" -PercentComplete $i
      $i++

      if (Get-InstalledModule -Name $module.Name -RequiredVersion $module.Version -ErrorAction SilentlyContinue @Params) {
        Write-Verbose -Message "Uninstalling $($module.Name) version $($module.Version)"

        Remove-Module -FullyQualifiedName @{ModuleName=$module.Name;ModuleVersion=$module.Version} -ErrorAction SilentlyContinue

        try {
          if ($PSCmdlet.ShouldProcess("$($module.Name) version $($module.Version)")) {
            Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -Force -ErrorAction Stop @Params
          }
          $State = 'Uninstalled'
        } Catch {
          $State = 'Manual uninstall required'
          Write-Verbose -Message "$($module.Name) version: $($module.Version) may require manual uninstallation."
        }

      } else {
        $State = 'Not found'
        Write-Verbose -Message "$($module.Name) version: $($module.Version) not found."
      }

      if (-not $PSBoundParameters.WhatIf) {
        [pscustomobject]@{
          ModuleName = $module.Name
          Version = $module.Version
          State = $State
        }
      }

    }
  }
}

若要使用此函式,請將程式碼複製並貼到 PowerShell 會話中。 下列範例示範如何執行 函式來移除舊版 Az PowerShell 模組及其子模組。

Azure PowerShell
Uninstall-AzModule -Name Az -Version 1.8.0

當腳本執行時,它會顯示 卸載之每個子模組的名稱、 版本 狀態 。 若要執行腳本,只查看將刪除的內容,而不移除它,請指定 -WhatIf 參數。

輸出
ModuleName              Version  State
----------              -------  -----
Az.Accounts             1.5.1    Not found
Az.Aks                  1.0.1    Uninstalled
Az.AnalysisServices     1.1.0    Uninstalled
Az.ApiManagement        1.0.0    Uninstalled
Az.ApplicationInsights  1.0.0    Uninstalled
...

重要

如果此腳本無法與要卸載的相同版本完全相依性相符,則不會卸載 該相依性的任何 版本。 這是因為您的系統上可能有其他版本的目的模組依賴這些相依性。

針對您要卸載的每個 Az PowerShell 模組版本執行下列範例。 為了方便起見,下列腳本會卸載 Az 的所有版本,但最新版本除外

PowerShell
$Modules = Get-InstalledModule -Name Az -AllVersions |
    Sort-Object -Property Version -Descending |
        Select-Object -Skip 1
$Modules | ForEach-Object {Uninstall-AzModule -Name $_.Name -Version $_.Version}

卸載 AzureRM 模組

如果您的系統上已安裝 Az 模組,而且想要卸載 AzureRM,則有兩個選項。 您遵循的哪個方法取決於您安裝 AzureRM 模組的方式。 如果您不確定原始的安裝方法,請先遵循卸載 MSI 的步驟。

從 MSI 卸載 AzureRM PowerShell 模組

如果您使用 MSI 套件安裝 AzureRM PowerShell 模組,您必須透過 Windows 系統卸載,而不是 PowerShell。

平台 Instructions
Windows 10 啟動 > 設定 > 應用程式
Windows 7
Windows 8
[開始] > [控制台] > [程式集] > [解除安裝程式]

在此畫面上,您應該會在計畫清單中看到 Azure PowerShell Microsoft Azure PowerShell - 月份年 。 這是要卸載的應用程式。 如果您沒有看到此程式列出,則您透過 PowerShellGet 安裝,並應遵循下一組指示。

從 PowerShellGet 卸載 AzureRM PowerShell 模組

如果您使用 PowerShellGet 安裝 AzureRM,則可以使用 Uninstall-AzureRM Cmdlet 移除模組 ,作為模組的 Az.Accounts 一部分。 下列範例會 從您的電腦中移除所有 AzureRM 模組。 它需要系統管理員許可權。

Azure PowerShell
Uninstall-AzureRm