You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell Core与Windows PowerShell间序列化ConfigCi模块数据?

在PowerShell 7.3+中使用ConfigCi模块的New-CIPolicyRule的解决方案

问题背景

需要在PowerShell 7.3+模块中调用ConfigCi内置模块的New-CIPolicyRule cmdlet,但该cmdlet仅兼容Windows PowerShell,在PowerShell Core中直接运行以下代码会失败:

$ScanLocation = "C:\Program Files\WindowsApps\*"
$RulesWildCards = New-CIPolicyRule -FilePathRule $ScanLocation
New-CIPolicy -MultiplePolicyFormat -FilePath .\policy.xml -Rules $RulesWildCards

报错信息:

New-CIPolicy: Cannot bind parameter 'Rules'. Cannot convert value "Microsoft.SecureBoot.UserConfig.Rule" to type "Microsoft.SecureBoot.UserConfig.Rule". Error: "Cannot convert the "Microsoft.SecureBoot.UserConfig.Rule" value of type "Deserialized.Microsoft.SecureBoot.UserConfig.Rule" to type "Microsoft.SecureBoot.UserConfig.Rule"."

尝试直接调用Windows PowerShell、使用Export-Clixml/Import-Clixml序列化数据的方法均未解决问题。

通用解决方案

核心思路是将涉及ConfigCi模块的完整逻辑放在Windows PowerShell会话中执行,避免跨PowerShell环境的对象序列化问题,PowerShell Core仅负责触发执行。

方案1:直接生成策略文件

如果仅需要生成最终的policy.xml文件,可通过PowerShell Core调用powershell.exe执行完整脚本:

# 定义要在Windows PowerShell中执行的脚本逻辑
$ciPolicyScript = @'
$ScanLocation = "C:\Program Files\WindowsApps\*"
$RulesWildCards = New-CIPolicyRule -FilePathRule $ScanLocation
New-CIPolicy -MultiplePolicyFormat -FilePath .\policy.xml -Rules $RulesWildCards
'@

# 调用Windows PowerShell执行脚本
powershell.exe -Command $ciPolicyScript

方案2:获取规则数据用于后续处理

如果需要在PowerShell Core中获取规则数据进行后续操作,可让Windows PowerShell将规则转换为JSON格式输出,再在PowerShell Core中解析:

$ciRuleScript = @'
$ScanLocation = "C:\Program Files\WindowsApps\*"
$RulesWildCards = New-CIPolicyRule -FilePathRule $ScanLocation
# 转换为JSON(指定Depth确保完整序列化)
$RulesWildCards | ConvertTo-Json -Depth 10
'@

# 获取JSON并转换为PowerShell对象
$rulesInCore = powershell.exe -Command $ciRuleScript | ConvertFrom-Json

原理说明

ConfigCi模块的Microsoft.SecureBoot.UserConfig.Rule类型无法在PowerShell Core和Windows PowerShell之间跨环境序列化/反序列化,导致类型转换失败。通过将所有涉及该模块的操作放在原生Windows PowerShell会话中执行,彻底规避了跨环境对象传递的问题;若需要数据交互,使用JSON/XML等通用格式传递,而非强类型对象。

内容的提问来源于stack exchange,提问作者SpyNet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 09:37:31