执行Get-DscConfigurationStatus报错Invalid namespace,DSC版本配置求助
PowerShell DSC "Invalid namespace" 错误排查与解决
执行
Get-DscConfigurationStatus时遭遇如下错误:PS C:\windows\system32> Get-DscConfigurationStatus Get-DscConfigurationStatus : Invalid namespace At line:1 char:1 + Get-DscConfigurationStatus + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : MetadataError: (MSFT_DSCLocalConfigurationManager:root/Microsoft/...gurationManager) [Get-DscConfigurationSt atus], CimException + FullyQualifiedErrorId : HRESULT 0x8004100e,Get-DscConfigurationStatus
问题根源
这个错误核心是WMI命名空间root/Microsoft/Windows/DesiredStateConfiguration损坏或未正确注册,和你提到的版本冲突直接相关:
- PowerShell ISE默认使用的PowerShell 5.1,自带的PSDesiredStateConfiguration 1.1属于传统DSC,依赖WMI命名空间运行
- PowerShell 7适配的PSDesiredStateConfiguration 2.x是DSCv2,基于CIM实现,和传统DSC的WMI命名空间不兼容
- 强制在PowerShell 5.1环境中安装2.x版本,会覆盖或破坏传统DSC的WMI组件,导致依赖该命名空间的命令失效
解决步骤
1. 清理冲突的模块版本
打开PowerShell ISE(确保是PowerShell 5.1环境),执行以下操作:
- 卸载PSDesiredStateConfiguration 2.0.7版本:
Uninstall-Module -Name PSDesiredStateConfiguration -RequiredVersion 2.0.7 -Force - 确认仅保留系统自带的1.1版本:
正常输出应只显示版本1.1,路径为Get-Module -Name PSDesiredStateConfiguration -ListAvailableC:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration
2. 修复WMI DSC命名空间
如果卸载后错误仍存在,需要重新注册DSC的WMI组件:
以管理员身份打开PowerShell 5.1,执行:
$dscNamespace = "root/Microsoft/Windows/DesiredStateConfiguration" # 删除损坏的命名空间 Remove-WmiObject -Namespace root -Class __Namespace -Filter "Name='Microsoft/Windows/DesiredStateConfiguration'" -ErrorAction SilentlyContinue # 重新注册DSC核心MOF文件 & "$env:windir\system32\wbem\mofcomp.exe" "$env:windir\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\MSFT_DSCLocalConfigurationManager.mof"
执行完成后,再次运行Get-DscConfigurationStatus验证是否恢复正常
3. 区分版本使用DSC
- PowerShell 5.1(ISE):仅使用系统自带的PSDesiredStateConfiguration 1.1,搭配已安装的PSDscResources 2.12.0.0(两者兼容)
- PowerShell 7:单独安装PSDesiredStateConfiguration 2.x,同时使用适配DSCv2的资源模块(避免混用传统DSC资源)
- 禁止在同一个PowerShell版本环境中混合安装不同大版本的PSDesiredStateConfiguration
内容的提问来源于stack exchange,提问作者Ryan
相关产品推荐
相关产品推荐

