脱机安装 Microsoft Entra PowerShell

在具有受限 Internet 访问的环境中,可能需要脱机安装 Microsoft Entra PowerShell 模块。 本文介绍如何使用下载的包文件和本地存储库设置和安装模块。

脱机安装适用于以下情况:

  • 不连接互联网的气隙网络
  • 具有限制 Internet 访问的严格安全策略的组织
  • 无法直接访问 PowerShell 库 的环境

先决条件

在开始之前,请确保具备:

  • PowerShell 5.1 或更高版本 (建议使用 PowerShell 7+ )
  • 目标计算机上的管理权限
  • 使用 Internet 连接访问下载位置以下载所需的文件
  • 来自 Microsoft Entra PowerShell 发布页面的已签名 nupkg 文件

脱机安装过程概述

脱机安装过程涉及以下主要步骤:

  1. 安装 Microsoft Graph PowerShell 依赖项
  2. 设置本地 PowerShell 存储库
  3. 在本地存储库中下载并放置 nupkg 文件
  4. 从本地存储库安装Microsoft Entra模块

安装 Microsoft Graph PowerShell 依赖项

Microsoft Entra PowerShell 模块依赖于特定的 Microsoft Graph PowerShell 模块。 从PowerShell 库安装这些依赖项时会自动安装,但在脱机方案中,需要手动安装它们。

Important

必须先在具有 Internet 访问权限的计算机上安装这些依赖项,或者通过组织的批准的软件分发方法获取依赖项模块。

运行以下脚本以安装所需的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

后续步骤