windows - 将变量作为参数传递给 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。
谢谢
解决方案
我猜这不喜欢你传递一个字符串的事实 - 尝试传递一个数组数组:
$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
- 至于评论中的错误,嗯...
推荐阅读
- json - 如何在 Swift 中解码带有数组根的 JSON
- visual-studio - 更新/清理离线 Visual Studio 安装程序
- java - 应用程序因“E/RecyclerView:未连接适配器;跳过布局”而不断崩溃
- javascript - 序列号的正则表达式
- python - 如何检查字符串中某个字符的多次出现
- css - next-css 导致无限编译
- docker - Docker 镜像的大小随着文件的变化而增长
- mongodb - 返回至少与 mongodb 集合中的 2 个属性匹配的数据
- java - 如何阻止 IntelliJ IDEA 在多行语句中添加额外的缩进?
- python - 在 matplotlib 的图中更改轴的索引