简体   繁体   English

使用Powershell或Powershell所需状态配置将Binary Registry Key设置为GUID值

[英]Set Binary Registry Key to GUID Value using Powershell or Powershell Desired State Configuration

We were instructed to update a REG_BINARY registry key using a GUID value, but we want to do this using PowerShell or PowerShell DSC so it can be automated. 我们被指示使用GUID值更新REG_BINARY注册表项,但是我们希望使用PowerShell或PowerShell DSC进行此操作,以便可以将其自动化。 The end result should look like this: 最终结果应如下所示:

REG_BINARY键设置为GUID值

As that is the hex representation of the GUID: 因为这是GUID的十六进制表示:

GUID: {01234567-89AB-CDEF-0123-456789ABCDEF}
Hex:  67-45-23-01-AB-89-EF-CD-01-23-45-67-89-AB-CD-EF

The following will create a new binary key using PowerShell 下面将使用PowerShell创建一个新的二进制密钥

$value = [guid]::Parse('01234567-89AB-CDEF-0123-456789ABCDEF')
$path = 'HKLM:Software\Company\Product'
New-ItemProperty -Path $path -Name MyGUIDKey -PropertyType Binary -Value $value.ToByteArray()

If the key already exists you can update it's value by replacing the last line with 如果密钥已经存在,您可以通过将最后一行替换为来更新其值

Set-ItemProperty -Path $path -Name MyGUIDKey -Value $value.ToByteArray()

If you want to create or update they key using PowerShell DSC your configuration should look like: 如果要使用PowerShell DSC创建或更新它们,则您的配置应如下所示:

Registry SetBinaryKeyToGuidValue
{
    Key = 'HKEY_LOCAL_MACHINE\Software\Company\Product'
    ValueName = 'MyGUIDKey'
    ValueData =  @([BitConverter]::ToString([guid]::Parse('01234567-89AB-CDEF-0123-456789ABCDEF').ToByteArray()).Replace("-", [String]::Empty))
    ValueType = 'Binary'
}

The ValueData is very particular about what format is used, and it should be and Array of Hex strings like: @('001122FF'). ValueData对于使用哪种格式非常特定,应该使用格式和十六进制字符串数组,例如:@('001122FF')。 If you use any other format you will get an error message like: 如果您使用任何其他格式,您将收到以下错误消息:

PowerShell DSC resource MSFT_RegistryResource failed to execute Set-TargetResource functionality with error message: (ERROR) Parameter 'ValueData' has an invalid value '01234567-89AB-CDEF-0123-456789ABCDEF' for type 'Binary' PowerShell DSC资源MSFT_RegistryResource无法执行带有错误消息的Set-TargetResource功能:(ERROR)参数'ValueData'的类型'Binary'具有无效的值'01234567-89AB-CDEF-0123-456789ABCDEF'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM