O formato do DSC MSFT_Registry
Tipo binário ValueData
é uma cadeia com pares contíguos de valores de byte, com um líder opcional "0x"
O truque é esse pequeno código do recurso em $ env: windir \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Módulos \ PSDesiredStateConfiguration \ DSCResources \ MSFT_Registry.psm1.
Analisa o valor com:
$binaryVal = $null
$val = $Data[0].TrimStart("0x")
if ($val.Length % 2 -ne 0)
{
$val = $val.PadLeft($val.Length+1, "0")
}
try
{
$byteArray = [Byte[]]@()
for ($i = 0 ; $i -lt ($val.Length-1) ; $i = $i+2)
{
$byteArray += [Byte]::Parse($val.Substring($i, 2), "HexNumber")
}
$ReturnValue.Value = [Byte[]]$byteArray
}
Exemplo
Usando uma pequena variação nos dados de entrada:
$reg = "01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,
14,00,00,00,02,00,50,00,03,00,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,
00,00,05,20,00,00,00,20,02,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,00,
00,05,20,00,00,00,25,02,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,00,00,
05,20,00,00,00,27,02,00,00,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,
00,00,00,05,12,00,00,00"
Você pode criar uma string formatada corretamente com isso:
$val = [String]::Join("",$reg.Split(",").Trim())
No MOF, você verá isso:
ValueData = {
"010004806400000070000000000000001400000002005000030000000000180003000f00010200000000000520000000200200000000180003000f00010200000000000520000000250200000000180003000f0001020000000000052000000027020000010100000000000512000000010100000000000512000000"
};
Aqui está uma amostra de teste inteira para provar o ponto:
$reg = "01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,
14,00,00,00,02,00,50,00,03,00,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,
00,00,05,20,00,00,00,20,02,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,00,
00,05,20,00,00,00,25,02,00,00,00,00,18,00,03,00,0f,00,01,02,00,00,00,00,00,
05,20,00,00,00,27,02,00,00,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,
00,00,00,05,12,00,00,00"
$val = [String]::Join("",$reg.Split(",").Trim())
Configuration RegistryTest
{
param([string] $Path, [string] $Name, [string] $BinaryValue)
Import-DscResource -ModuleName PSDesiredStateConfiguration
Registry test
{
Key = $Path
ValueName = $Name
ValueData = $val
ValueType = "Binary"
}
}
$args = @{
Path = "HKLM:\SOFTWARE\StackOverflow"
Name = "BinaryTest"
}
Remove-ItemProperty @args
Get-ItemProperty @args -ErrorAction Continue # Nothing up my sleeve!
$o = RegistryTest @args -BinaryValue $val -outputpath $env:temp
Start-DscConfiguration ($o | split-path) -Wait -Verbose -force
Get-ItemProperty @args
Isso resulta na seguinte saída:
Get-ItemProperty : Property BinaryTest does not exist at path
HKEY_LOCAL_MACHINE\SOFTWARE\StackOverflow.
At C:\scratch\test.ps1:30 char:1
+ Get-ItemProperty @args -ErrorAction Continue # Nothing up my sleeve!
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (BinaryTest:String) [Get-ItemProperty], PSArgu
mentException
+ FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.Powe
rShell.Commands.GetItemPropertyCommand
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendCo
nfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microso
ft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer STACKOVERFLOW with user sid S-...
VERBOSE: [STACKOVERFLOW]: LCM: [ Start Set ]
VERBOSE: [STACKOVERFLOW]: LCM: [ Start Resource ] [[Registry]test]
VERBOSE: [STACKOVERFLOW]: LCM: [ Start Test ] [[Registry]test]
VERBOSE: [STACKOVERFLOW]: [[Registry]test] Registry key value 'HKLM:\SOFTW
ARE\StackOverflow\BinaryTest' does not exist
VERBOSE: [STACKOVERFLOW]: LCM: [ End Test ] [[Registry]test] in 0.2130 seconds.
VERBOSE: [STACKOVERFLOW]: LCM: [ Start Set ] [[Registry]test]
VERBOSE: [STACKOVERFLOW]: [[Registry]test] (SET) Set registry key value 'H
KLM:\SOFTWARE\StackOverflow\BinaryTest' to '(1, 0, 4, 128, 100, 0, 0, 0, 112, 0, 0, 0, 0, 0, 0,
0, 20, 0, 0, 0, 2, 0, 80, 0, 3, 0, 0, 0, 0, 0, 24, 0, 3, 0, 15, 0, 1, 2, 0, 0, 0, 0, 0, 5, 32,
0, 0, 0, 32, 2, 0, 0, 0, 0, 24, 0, 3, 0, 15, 0, 1, 2, 0, 0, 0, 0, 0, 5, 32, 0, 0, 0, 37, 2, 0,
0, 0, 0, 24, 0, 3, 0, 15, 0, 1, 2, 0, 0, 0, 0, 0, 5, 32, 0, 0, 0, 39, 2, 0, 0, 1, 1, 0, 0, 0,
0, 0, 5, 18, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 5, 18, 0, 0, 0)' of type 'Binary'
VERBOSE: [STACKOVERFLOW]: LCM: [ End Set ] [[Registry]test] in 0.1390 seconds.
VERBOSE: [STACKOVERFLOW]: LCM: [ End Resource ] [[Registry]test]
VERBOSE: [STACKOVERFLOW]: LCM: [ End Set ]
VERBOSE: [STACKOVERFLOW]: LCM: [ End Set ] in 0.7010 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 0.799 seconds
BinaryTest : {1, 0, 4, 128...}
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\StackOverflow
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName : StackOverflow
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
Olhando para um servidor 2016 e uma máquina com o Windows 10 em que executei isso, o registro parece correto.