離線安裝 Microsoft Entra PowerShell

在網路受限的環境中,你可能需要離線安裝 Microsoft Entra PowerShell 模組。 本文說明如何使用下載的套件檔案和本地倉庫來設定和安裝模組。

離線安裝在以下情境下非常有用:

  • 沒有網路連線的空隙網路
  • 有嚴格安全政策限制網路存取的組織
  • 無法直接存取 PowerShell 資源庫 的環境

先決條件

在開始之前,請確保您擁有:

  • PowerShell 5.1 或以上(建議使用 PowerShell 7+)
  • 目標機器上的管理權限
  • 存取下載位置並連網下載所需檔案
  • 已簽署的 nupkg 檔案源自於 Microsoft Entra PowerShell 發行頁面

離線安裝流程概述

離線安裝過程主要包含以下步驟:

  1. 安裝 Microsoft Graph PowerShell 相依關係
  2. 建立一個本地的 PowerShell 倉庫
  3. 下載並放置 nupkg 檔案到本地倉庫
  4. 從本地倉庫安裝 Microsoft Entra 模組

安裝 Microsoft Graph PowerShell 相依關係

Microsoft Entra PowerShell 模組依賴特定的 Microsoft Graph PowerShell 模組。 這些相依在從 PowerShell 資源庫 安裝時會自動安裝,但在離線情況下,則需要手動安裝。

Important

您必須先在有網路連線的機器上安裝這些相依模組,或透過貴組織核准的軟體發行方式取得相依模組。

執行以下腳本安裝所需的 Microsoft Graph PowerShell 相依關係。 Microsoft Entra PowerShell 目前支援版本 2.25.0

# Define the required Graph modules version
$graphModulesVersion = "2.25.0"

# List of required Graph modules
$graphModules = @(
    "Microsoft.Graph.DirectoryObjects",
    "Microsoft.Graph.Users",
    "Microsoft.Graph.Users.Actions",
    "Microsoft.Graph.Groups",
    "Microsoft.Graph.Identity.DirectoryManagement",
    "Microsoft.Graph.Identity.Governance",
    "Microsoft.Graph.Identity.SignIns",
    "Microsoft.Graph.Applications",
    "Microsoft.Graph.Reports"
)

# Install each module
foreach ($moduleName in $graphModules) {
    Write-Host "Installing Module $($moduleName)" -ForegroundColor Green
    Install-Module $moduleName -Scope CurrentUser -RequiredVersion $graphModulesVersion -Force -AllowClobber -Verbose
}

建立一個本地的 PowerShell 倉庫

  1. 建立一個本地 PowerShell 倉庫,存放 Microsoft Entra PowerShell 模組套件。

    # Define local gallery settings
    $localGallery = '__LocalGallery__'
    $galleryPath = 'C:\LocalPowerShellGallery'
    
    # Create the gallery directory if it doesn't exist
    if (-not(Test-Path $galleryPath)) {
        Write-Host "Creating local gallery directory: $galleryPath" -ForegroundColor Yellow
        $null = New-Item -Path $galleryPath -ItemType Directory
    }
    
    # Register the local repository
    Write-Host "Registering local PowerShell repository: $localGallery" -ForegroundColor Green
    $null = Register-PSRepository -Name $localGallery -SourceLocation $galleryPath -ScriptSourceLocation $galleryPath -InstallationPolicy Trusted
    
  2. 確認本地儲存庫已成功建立:

    # Display all registered repositories
    Get-PSRepository
    

    你應該會看到類似的輸出:

    Name                      InstallationPolicy   SourceLocation
    ----                      ------------------   --------------
    PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2
    __LocalGallery__          Trusted              C:\LocalPowerShellGallery
    

下載並放置 nupkg 檔案

下載 nupkg 檔案

  1. 請前往 GitHub 上的 Microsoft Entra PowerShell 發布頁面

  2. 選擇你想安裝的版本(例如 v1.0.11)。

  3. 資產 區塊,根據你的需求下載所需的 nupkg 檔案:

    僅限於 v1.0 模組:

    • Microsoft.Entra.1.0.11.nupkg
    • Microsoft.Entra.Applications.1.0.11.nupkg
    • Microsoft.Entra.Authentication.1.0.11.nupkg
    • Microsoft.Entra.CertificateBasedAuthentication.1.0.11.nupkg
    • Microsoft.Entra.DirectoryManagement.1.0.11.nupkg
    • Microsoft.Entra.Governance.1.0.11.nupkg
    • Microsoft.Entra.Groups.1.0.11.nupkg
    • Microsoft.Entra.Reports.1.0.11.nupkg
    • Microsoft.Entra.SignIns.1.0.11.nupkg
    • Microsoft.Entra.Users.1.0.11.nupkg

    對於 Beta 模組: 如果你需要 Beta 功能,請下載對應 .Beta. 的 nupkg 檔案。

將檔案放入本地儲存庫

將所有下載的 nupkg 檔案複製到你本地的畫廊資料夾:

# Copy nupkg files to the local gallery
# Replace 'C:\Downloads' with the actual path where you downloaded the files
$downloadPath = 'C:\Downloads'
$nupkgFiles = Get-ChildItem -Path $downloadPath -Filter "Microsoft.Entra*.nupkg"

foreach ($file in $nupkgFiles) {
    Write-Host "Copying $($file.Name) to local gallery" -ForegroundColor Cyan
    Copy-Item -Path $file.FullName -Destination $galleryPath -Force
}

步驟 4:安裝 Microsoft Entra PowerShell 模組

安裝個別模組

你可以根據自己的需求單獨安裝模組:

# Install a specific module
Install-Module -Name 'Microsoft.Entra.Authentication' -Repository $localGallery -AllowClobber -Verbose

安裝所有模組

要安裝所有可用的 Microsoft Entra 模組,請使用此腳本:

# Define the modules to install
$entraModules = @(
    "Microsoft.Entra.Authentication",
    "Microsoft.Entra.Applications",
    "Microsoft.Entra.DirectoryManagement",
    "Microsoft.Entra.Groups",
    "Microsoft.Entra.Governance",
    "Microsoft.Entra.SignIns",
    "Microsoft.Entra.Users",
    "Microsoft.Entra.Reports",
    "Microsoft.Entra.CertificateBasedAuthentication"
)

# Install each module from the local gallery
foreach ($moduleName in $entraModules) {
    Write-Host "[Local Gallery] Installing: $moduleName" -ForegroundColor Green
    Install-Module -Name $moduleName -Repository $localGallery -AllowClobber -Verbose
}

安裝根模組(可選)

安裝依賴模組後,你可以選擇安裝根模組:

Install-Module -Name 'Microsoft.Entra' -Repository $localGallery -AllowClobber -Verbose

確認安裝

確認模組安裝成功:

# Display all installed Microsoft Entra modules
Get-InstalledModule Microsoft.Entra.*

你應該會看到類似的輸出:

Version      Name                                            Repository           Description
-------      ----                                            ----------           -----------
1.0.11       Microsoft.Entra.Applications                    __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.Authentication                  __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.CertificateBasedAuthentication  __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.DirectoryManagement             __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.Governance                      __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.Groups                          __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.Reports                         __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.SignIns                         __LocalGallery__     Microsoft Entra PowerShell
1.0.11       Microsoft.Entra.Users                           __LocalGallery__     Microsoft Entra PowerShell

測試安裝

請透過連接 Microsoft Entra 來測試模組是否正常運作:

# Import the authentication module
Import-Module Microsoft.Entra.Authentication

# Connect to Microsoft Entra (this will require internet connectivity for authentication)
Connect-Entra

# Test with a simple command
Get-EntraContext

清理(可選)

取消註冊本地儲存庫

如果你不再需要本地儲存庫,可以取消註冊:

# Unregister the local repository
Unregister-PSRepository -Name $localGallery
Write-Host "Local repository '$localGallery' has been unregistered" -ForegroundColor Yellow

若要完全移除本機相簿:

# Remove the local gallery folder and its contents
if (Test-Path $galleryPath) {
    Remove-Item -Path $galleryPath -Recurse -Force
    Write-Host "Local gallery folder removed: $galleryPath" -ForegroundColor Yellow
}

Troubleshooting

常見問題和解決方案

問題 原因 解決方案
模組未在儲存庫中找到 NUPKG 檔案未複製至本地畫廊 驗證檔案是否在正確的畫廊路徑上
尚未安裝相依性 缺少 Graph PowerShell 模組 先安裝所需的 Microsoft Graph PowerShell 模組
存取被拒絕 權限不足 以系統管理員身分執行 PowerShell
儲存庫不值得信任 本地儲存庫未被標記為受信任 使用 -Force 參數或將安裝政策設為可信

驗證本地儲存庫內容

要查看你本地倉庫中有哪些套件可用:

# List all available packages in the local repository
Find-Module -Repository $localGallery

檢查模組相依關係

要理解模組需要哪些相依關係:

# Get module dependencies
Find-Module -Name "Microsoft.Entra.Users" -Repository $localGallery | Select-Object -ExpandProperty Dependencies

下一步