首页 > 解决方案 > 将变量作为参数传递给 Set-MpPreference

问题描述

我正在尝试通过 Set-MpPreference 配置 Windows Defender。

这是我的代码:

$ASRIds = "01443614-cd74-433a-b99e-2ecdc07bfc25,92E97FA1-2EDF-4476-BDD6-9DD0B4DDDC7B"
$ASRValues = "1,1"

Set-MpPreference -AttackSurfaceReductionRules_Ids $ASRIds -AttackSurfaceReductionRules_Actions $ASRValues

但是,我收到错误

Set-MpPreference : Cannot process argument transformation on parameter 'AttackSurfaceReductionRules_Actions'. Cannot
convert value "1,1" to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes.MpPreference.ASRRuleActionType[]".
Error: "Cannot convert value "1,1" to type
"Microsoft.PowerShell.Cmdletization.GeneratedTypes.MpPreference.ASRRuleActionType". Error: "Unable to match the
identifier name 1,1 to a valid enumerator name. Specify one of the following enumerator names and try again:
Disabled, Enabled, AuditMode""
At line:1 char:96
+ ... tionRules_Ids $ASRIds -AttackSurfaceReductionRules_Actions $ASRValues
+                                                                ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-MpPreference], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MpPreference

我知道这可以像这样直接完成:

Set-MpPreference -AttackSurfaceReductionRules_Ids 1443614-cd74-433a-b99e-2ecdc07bfc25,92E97FA1-2EDF-4476-BDD6-9DD0B4DDDC7B -AttackSurfaceReductionRules_Actions 1,1

但将其作为变量传递是我项目的必要部分。我可以做些什么来使变量起作用吗?

一个接一个(当时只使用一个减少攻击面的规则)有效,但如果您只启用一个,Windows Defender 会禁用其他规则。所以我需要同时在变量中有多个ID。

谢谢

标签: windowspowershell

解决方案


我猜这不喜欢你传递一个字符串的事实 - 尝试传递一个数组数组:

$ASRIds = @("01443614-cd74-433a-b99e-2ecdc07bfc25","92E97FA1-2EDF-4476-BDD6-9DD0B4DDDC7B")
$ASRValues = @(1,1)

故障排除说明

  • 你的[]错误告诉我们它需要一个数组
...Microsoft.PowerShell.Cmdletization.GeneratedTypes.MpPreference.ASRRuleActionType[]...
  • 这部分告诉我们它正在尝试将整个字符串1,1与一个枚举器匹配——它应该一次匹配一个。
Unable to match the identifier name 1,1 to a valid enumerator name
  • 我猜你想指定Enabled?考虑这一点,以使您的代码更具可读性:$ASRValues = @('Enabled','Enabled')
Specify one of the following enumerator names and try again:
Disabled, Enabled, AuditMode
  • 至于评论中的错误,嗯...

推荐阅读