如何使用PowerShell设置二进制注册表值(REG_BINARY)?

bre*_*ers 17 registry powershell powershell-2.0

如何使用PowerShell设置二进制注册表值(REG_BINARY)?

背景:

我需要使用PowerShell脚本更改ASP.NET State服务的某些属性.遗憾的是,内置的PowerShell cmdlet Set-Service仅允许您修改服务描述,启动类型,显示名称和状态.我需要修改Subsequent failuresRecovery选项卡上的属性(查看服务的属性时).我发现此值作为REG_BINARY值存储在注册表中.

值的导出如下所示:

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\aspnet_state]
"FailureActions"=hex:50,33,01,00,00,00,00,00,00,00,00,00,03,00,00,00,0e,00,00,\
  00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00
Run Code Online (Sandbox Code Playgroud)

在Powershell中有一个Set-ItemPropertycmdlet,您可以使用该cmdlet设置注册表值.对于字符串或双字值,您只需传递字符串或int.我知道要更改的数组中的哪个十六进制值,但我无法弄清楚如何设置二进制值.

How*_*ard 20

以下行为您提供了如何创建一个示例

New-ItemProperty -Path . -Name Test -PropertyType Binary -Value ([byte[]](0x30,0x31,0xFF))
Run Code Online (Sandbox Code Playgroud)

以及如何更改现有的:

Set-ItemProperty -Path . -Name Test -Value ([byte[]](0x33,0x32,0xFF))
Run Code Online (Sandbox Code Playgroud)


Fri*_*der 6

这篇文章帮助我解决了类似的问题.谢谢!

将xBr0k3n和Howard的答案结合在一起:

#Change these three to match up to the extracted registry data and run as Admin
$YourInput = "50,33,01,00,00,00,00,00,00,00,00,00,03,00,00,00,0e,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00"
$RegPath   = 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\aspnet_state'
$AttrName  = "FailureActions"

$hexified = $YourInput.Split(',') | % { "0x$_"}
New-ItemProperty -Path $RegPath -Name $AttrName -PropertyType Binary -Value ([byte[]]$hexified)
Run Code Online (Sandbox Code Playgroud)


xBr*_*k3n 5

只是我觉得这会错过这个问题的主要部分吗?

你会如何改变原作:

50,33,01,00,00,00,00,00,00,00,00,00,03,00,00,00,0e,00,00,\
00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00
Run Code Online (Sandbox Code Playgroud)

进入如下格式:

([byte[]](0x33,0x32,0xFF))
Run Code Online (Sandbox Code Playgroud)

编辑:在尝试使这个工作后,事实证明你只是用'0x'作为所有对的前缀.不确定答案中没有提到为什么.所以只需将以上内容更改为:

0x50,0x33,0x01,0x00,0x00,0x00,0x00,0x00... etc.
Run Code Online (Sandbox Code Playgroud)

然后将其包装在以下内容中:

([byte[]](0x50,0x33,0x01,0x00,0x00,0x00,0x00,0x00... etc.))
Run Code Online (Sandbox Code Playgroud)