mcpField
P
powershell-2025-changes

powershell-2025-changes

github.com/josiahsiegel开发59171 次安装19 周前更新
代码仓库

关于 powershell-2025-changes

面向管理员和开发者的迁移参考资料,汇总 2025 年所述的 PowerShell 变更。内容涵盖功能移除、模块迁移、安全建议,以及检查旧依赖的方法。

主要功能

  1. 01PowerShell 2.0 移除指南
  2. 02MSOnline 与 AzureAD 迁移路径
  3. 03WMIC 和包管理转换
  4. 04JSON 架构与 PSSnapin 变更
  5. 05安全与迁移检查清单

适用场景

  1. 01审查脚本中的弃用依赖
  2. 02将身份命令迁移到 Microsoft.Graph
  3. 03替换 WMIC 和 PowerShellGet 用法

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/josiahsiegel/claude-plugin-marketplace --skill powershell-2025-changes

安装后在 Claude Code 中输入/powershell-2025-changes确认已加载。

详细介绍

PowerShell 2025 重大变更与迁移

2025 年 PowerShell 的关键变更、弃用事项和迁移路径。

移除 PowerShell 2.0(2025 年 8 月至 9 月)

移除内容

PowerShell 2.0 已从以下系统中彻底移除:

  • Windows 11 版本 24H2(2025 年 8 月)
  • Windows Server 2025(2025 年 9 月)

原因: 安全性改进、缩小攻击面、清理旧版代码

迁移路径
# Check if PowerShell 2.0 is installed
Get-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV2Root

# If you still need PowerShell 2.0 (NOT RECOMMENDED)
# - Use older Windows versions
# - Use Windows containers with older base images
# - Upgrade scripts to PowerShell 5.1 or 7+

# Recommended: Migrate to PowerShell 7.5+
winget install Microsoft.PowerShell

需要采取的措施: 审查所有脚本,并从任何 PowerShell 调用中移除 -Version 2.0 参数。


MSOnline 和 AzureAD 模块退役

退役时间表
模块停止工作时间完成退役时间
MSOnline2025 年 5 月下旬2025 年 5 月 31 日
AzureAD2025 年 3 月 30 日2025 年 7 月 1 日之后

重要提示: 这些模块将停止运行——不仅仅是弃用,而是彻底无法使用。

迁移路径

从 MSOnline/AzureAD 迁移到 Microsoft.Graph:

# OLD (MSOnline) - STOPS WORKING MAY 2025
Connect-MsolService
Get-MsolUser
Set-MsolUser -UserPrincipalName "[email protected]" -UsageLocation "US"

# NEW (Microsoft.Graph 2.32.0)
Connect-MgGraph -Scopes "User.ReadWrite.All"
Get-MgUser
Update-MgUser -UserId "[email protected]" -UsageLocation "US"

# OLD (AzureAD) - STOPS WORKING MARCH 2025
Connect-AzureAD
Get-AzureADUser
New-AzureADUser -DisplayName "John Doe" -UserPrincipalName "[email protected]"

# NEW (Microsoft.Graph 2.32.0)
Connect-MgGraph -Scopes "User.ReadWrite.All"
Get-MgUser
New-MgUser -DisplayName "John Doe" -UserPrincipalName "[email protected]"

替代方案: 使用 Microsoft Entra PowerShell 模块(AzureAD 的后续模块)

Install-Module -Name Microsoft.Graph.Entra -Scope CurrentUser
Connect-Entra
Get-EntraUser
常见命令映射
MSOnline/AzureADMicrosoft.Graph备注
Get-MsolUser / Get-AzureADUserGet-MgUser需要 User.Read.All 作用域
Get-MsolGroup / Get-AzureADGroupGet-MgGroup需要 Group.Read.All 作用域
Get-MsolDevice / Get-AzureADDeviceGet-MgDevice需要 Device.Read.All 作用域
Connect-MsolService / Connect-AzureADConnect-MgGraph基于作用域的权限

移除 WMIC(Windows 11 25H2)

移除内容

升级到 Windows 11 25H2+ 后,Windows Management Instrumentation Command-line (WMIC) 工具将被移除。

迁移路径

从 WMIC 迁移到 PowerShell WMI/CIM:

# OLD (WMIC) - REMOVED
wmic process list brief
wmic os get caption

# NEW (PowerShell CIM)
Get-CimInstance -ClassName Win32_Process | Select-Object Name, ProcessId, CommandLine
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, Version

# For detailed process info
Get-Process | Format-Table Name, Id, CPU, WorkingSet -AutoSize

# For system info
Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion

从 PowerShellGet 迁移到 PSResourceGet

现代包管理(2025)

PSResourceGet 是 PowerShellGet 的官方后续工具(速度快 2 倍,且仍在积极开发)。

# Install PSResourceGet (ships with PowerShell 7.4+)
Install-Module -Name Microsoft.PowerShell.PSResourceGet -Force

# New commands (PSResourceGet)
Install-PSResource -Name Az -Scope CurrentUser  # Replaces Install-Module
Find-PSResource -Name "*Azure*"                 # Replaces Find-Module
Update-PSResource -Name Az                      # Replaces Update-Module
Get-InstalledPSResource                         # Replaces Get-InstalledModule

# Compatibility layer available for legacy scripts
# Your old Install-Module commands still work but call PSResourceGet internally

性能对比:

  • PowerShellGet:安装模块需要 10-15 秒
  • PSResourceGet:安装模块需要 5-7 秒(速度快 2 倍)

Test-Json 架构变更

重大变更(PowerShell 7.4+)

Test-Json 现在使用 JsonSchema.NET,而非 Newtonsoft.Json.Schema。

影响: 不再支持 Draft 4 JSON 架构。

# OLD (Draft 4 schema) - NO LONGER SUPPORTED
$schema = @"
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object"
}
"@

Test-Json -Json $json -Schema $schema  # FAILS in PowerShell 7.4+

# NEW (Draft 6+ schema) - SUPPORTED
$schema = @"
{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object"
}
"@

Test-Json -Json $json -Schema $schema  # WORKS

移除 #Requires -PSSnapin

重大变更(PowerShell 7.4+)

所有与 #Requires -PSSnapin 相关的代码均已移除。

# OLD (PowerShell 5.1 and earlier)
#Requires -PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

# NEW (Use modules instead)
#Requires -Modules ExchangeOnlineManagement

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline

安全强化(2025 标准)

Just Enough Administration(JEA)

JEA 现已成为生产环境的安全要求:

# Create JEA session configuration
New-PSSessionConfigurationFile -SessionType RestrictedRemoteServer `
    -Path "C:\JEA\RestrictedAdmin.pssc" `
    -VisibleCmdlets @{
        Name = 'Restart-Service'
        Parameters = @{ Name = 'Name'; ValidateSet = 'Spooler' }
    } `
    -LanguageMode NoLanguage

# Register JEA endpoint
Register-PSSessionConfiguration -Name RestrictedAdmin `
    -Path "C:\JEA\RestrictedAdmin.pssc" `
    -Force

# Connect with limited privileges
Enter-PSSession -ComputerName Server01 -ConfigurationName RestrictedAdmin
Windows Defender Application Control(WDAC)

WDAC 取代 AppLocker,用于控制 PowerShell 脚本:

# Create WDAC policy for PowerShell scripts
New-CIPolicy -FilePath "C:\WDAC\PowerShellPolicy.xml" `
    -ScanPath "C:\Scripts" `
    -Level FilePublisher `
    -Fallback Hash

# Convert to binary and deploy
ConvertFrom-CIPolicy -XmlFilePath "C:\WDAC\PowerShellPolicy.xml" `
    -BinaryFilePath "C:\Windows\System32\CodeIntegrity\SIPolicy.p7b"
受限语言模式

现在建议所有没有管理员权限的用户使用受限语言模式:

# Check current language mode
$ExecutionContext.SessionState.LanguageMode
# Output: FullLanguage (admin) or ConstrainedLanguage (standard user)

# Set system-wide constrained language mode via Group Policy or Environment Variable
# Set HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\__PSLockdownPolicy = 4

PowerShell 7.6 预览版功能

当前状态(2025 年 10 月)

PowerShell 7.6.0 Preview 5 已发布(基于 .NET 9.0.101 构建)

新功能:

  • PSRedirectToVariable:允许重定向到变量
  • 模块重命名:ThreadJob → Microsoft.PowerShell.ThreadJob
  • PSResourceGet 1.1.0:改进性能并支持 Azure Artifacts
# Check PowerShell version
$PSVersionTable.PSVersion
# 7.5.4 (stable) or 7.6.0-preview.5

# .NET version
[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
# .NET 9.0.101

迁移检查清单

需要立即采取的措施(2025 年)
  • 审查 MSOnline/AzureAD 的使用情况 - 在 2025 年 5 月之前迁移到 Microsoft.Graph 2.32.0
  • 移除 PowerShell 2.0 引用 - 升级到 PowerShell 7.5+
  • 替换 WMIC 命令 - 使用 Get-CimInstance/Get-Process
  • 更新 JSON 架构 - 从 Draft 4 迁移到 Draft 6+
  • 移除 PSSnapin 要求 - 转换为模块
  • 采用 PSResourceGet - 使用更快、更现代的包管理工具
  • 实施 JEA - 为生产环境启用基于角色的访问控制
  • 启用 WDAC - 控制 PowerShell 脚本的应用程序
  • 测试受限语言模式 - 面向非管理员用户
建议操作
  • 升级到 PowerShell 7.5.4 - 搭载最新稳定版 .NET 9
  • 采用 Az 14.5.0 - 支持区域冗余的最新 Azure 模块
  • 使用 Microsoft.Graph 2.32.0 - 持续维护的 Graph SDK
  • 启用脚本块日志记录 - 用于安全审计
  • 实施代码签名 - 用于生产脚本
  • 使用 Azure Key Vault - 用于凭据管理

测试迁移

# Test for deprecated module usage
Get-Module MSOnline, AzureAD -ListAvailable
# If found, plan migration immediately

# Test for PowerShell 2.0 dependencies
Get-Content "script.ps1" | Select-String -Pattern "powershell.exe -Version 2"
# If found, remove version parameter

# Test for WMIC usage
Get-ChildItem -Path "C:\Scripts" -Recurse -Filter "*.ps1" |
    Select-String -Pattern "wmic" |
    Select-Object Path, Line

# Verify PowerShell version compatibility
#Requires -Version 7.0
Test-Path $PSCommandPath  # Ensures script is PowerShell 7+

资源


最后更新: 2025 年 10 月

评论

支持 Markdown