什么应该是一个简单的registryDSCconfiguration已经变成一个有点令人沮丧的猜测。 我正在尝试设置二进制registry项。 我发现不可能find正确的值数据格式来获得正确的密钥集。 我试图将这个registry文件转换成DSC:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\DefaultSecurity] "SrvsvcShareAdminConnect"=hex: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
我已经试过这两个registry和XRegistry资源,并击中相同的格式错误(我真的不介意我使用)。 我已经尝试提供的数据作为一个单一的string,string数组,string数组附加到0x显示它是hex等。我也试过这里的build议。
我得到的最接近下面的configuration,似乎工作:
Registry disableAdminShare { Ensure = "Present" Key = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\DefaultSecurity" Force = $true ValueName = "SrvsvcShareAdminConnect" ValueData = @("010004806400000070000000000000001400000002005000030000000000180003000f00010200000000000520000000200200000000180003000f00010200000000000520000000250200000000180003000f0001020000000000052000000027020000010100000000000512000000010100000000000512000000") ValueType = "Binary" }
但是在应用日志的时候,看起来是把这个值转换成了十进制,导致一个无效的条目:
'HKLM:\SYSTEM\CurrentControlSet\Services\lanmanserver\DefaultSecurity\SrvsvcSha reAdminConnect' 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: [SCDEV-RD-02]: LCM: [ End Set ]
我确信有一个简单的答案,但我不能在文档中find任何东西。
DSC格式的MSFT_Registry二进制typesValueData是一个string,它具有连续的字节值对,可选的前导“0x”
诀窍是来自$ env:windir \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Modules \ PSDesiredStateConfiguration \ DSCResources \ MSFT_Registry.psm1资源的这一点代码。
它分析的价值:
$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 }
例
在input数据上稍微改变一下:
$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"
你可以用这个来创build一个格式正确的string:
$val = [String]::Join("",$reg.Split(",").Trim())
在财政部,你会看到这样的:
ValueData = { "010004806400000070000000000000001400000002005000030000000000180003000f00010200000000000520000000200200000000180003000f00010200000000000520000000250200000000180003000f0001020000000000052000000027020000010100000000000512000000010100000000000512000000" };
这是一个完整的testing样本来certificate这一点:
$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
这导致以下输出:
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
看看我运行这个服务器2016年和Windows 10机器,registry看起来是正确的。