【问题标题】:REG EXPORT single value to .reg fileREG EXPORT 单个值到 .reg 文件
【发布时间】:2021-05-06 22:07:58
【问题描述】:

我想使用 PowerShell 将单个注册表值导出到文件(最终编写脚本来备份一堆注册表值)。

我可以用QUERY查看文件:

REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v LimitBlankPasswordUse
>>
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
    LimitBlankPasswordUse    REG_DWORD    0x1

但是,我无法导出单个值。

REQ EXPORT 没有 /v 标志,所以:

REG EXPORT "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" \v "LimitBlankPasswordUse" LBPU.reg
ERROR: Invalid syntax.

没用。

如果我尝试导出我得到的值:

REG EXPORT "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\LimitBlankPasswordUse" LBPU.reg
ERROR: The system was unable to find the specified registry key or value.

如果我只是做文件夹:

REG EXPORT "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\" Lsa.reg
The operation completed successfully.

我得到一个 .reg 文件,其中包含正确的值:密钥对 "LimitBlankPasswordUse"=dword:00000001 - 以及所有其他的。这是没有用的。我想要一对,而不是数百。

如何将单个值导出到 .reg 文件?

【问题讨论】:

  • 答案是否定的。但是,如果您愿意使用 PowerShell,还有其他方法可以完成类似的事情。

标签: powershell command-line registry backup


【解决方案1】:

您在这里根本没有使用过 PowerShell。正如 zett42 所说,这是一个Get-ItemProperty 问题。如果您必须有一个.reg 文件作为输出,那么您可以轻松地创建一个,只要您有简单的情况,例如输出是单个dword。如果您有大量项目和更复杂的数据类型,则该方法将很困难。

$lsa = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa" -Name LimitBlankPasswordUse
$path = $lsa.PSPath.Split('::')
Set-Content foo.reg "Windows Registry Editor Version 5.00`r`n`r`n[$($path[2])]`r`n`"LimitBlankPasswordUse`"=dword:$($lsa.LimitBlankPasswordUse)"

根据您的总体目标,您可以根据需要尽可能多地硬编码或编写脚本。可能会导出为 CSV,然后使用 Set-ItemProperty 在目标上应用。

【讨论】:

  • 顺便说一句:要直接获取注册表值的数据,请使用Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\Lsa LimitBlankPasswordUseGet-ItemPropertyValue 是 PSv5+)
【解决方案2】:

没有直接方法可以实现你想要的,并且假设你想要一个 .reg 文件作为输出,使用 PowerShell 的 cmdlet(例如Get-ItemGet-ItemPropertyGet-ItemPropertyValue) 不是选项 - 除非您准备模拟 .reg 文件格式 在所有方面,这在工作量和复杂性方面都是不平凡的。[1]

在您的情况下,最好的方法是首先使用reg.exe export整个密钥导出到一个临时文件,然后然后仅提取和导出标题行和感兴趣的数据值对。

鉴于单个数据-值对可以跨越多行,这并不简单,但以下内容应该可以正常工作(您可以将其包装在 函数中为了方便和重复使用):

# Determine the key path and the name of the value to export...
$keyPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa'
$valueName = 'LimitBlankPasswordUse'
# ... and the output file path.
$outFile = 'LBPU.reg'

# Create a temporary file and export the whole key to it.
$tempFile = New-TemporaryFile
$null = reg.exe export $keyPath $tempFile /y

# Extract the header lines and the key's *immediate* value children.
# Note: reg.exe export invariably exports *recursively*, so that any 
#       descendant keys are exported as well, which we want to exclude.
$null = (Get-Content -Raw $tempFile) -match '(?s)^(.+?\])\r\n(.+?)\r\n(?:\r\n|\z)'
Remove-Item $tempFile

# Extract the header lines (format identifier and key path)...
$headerLinesBlock = $Matches[1]
# ... and the key's immediate children as value-data pairs
$valueLinesBlock = $Matches[2]

# Extract the specific value-data pair of interest.
# Note: Such a pair can span *multiple* lines, if "\" is used as the
#       line-continuation character at the end of a line, as is common
#       with hex. data.
#       Continuation lines always start with a *space*, so a data-value pair
#       ends either with a new line whose first char. is a non-whitespace char. 
#       or with the end of the input string.
if ($valueLinesBlock -notmatch "(?sm)^`"$valueName`"=.+?(?=(\r\n\S|\z))") {
  throw "Value name not found: $valueName"
}
$valueDataPair = $Matches[0]

# Save the header lines and the data-value pair line(s) to the result file.
# Note that .reg files are "Unicode" (UTF-16LE) files.
$headerLinesBlock, $valueDataPair | Set-Content -Encoding Unicode $outFile

$outFile收到以下内容:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
"LimitBlankPasswordUse"=dword:00000001

[1] 举几个挑战:REG_DWORDREG_QWORD 值必须表示为 hex 值,REG_EXPAND_SZ 值在使用 Get-ItemProperty 查询时总是扩展,因此您不会保留存储在注册表中的实际值;此外,REG_EXPAND_SZREG_MULTI_SZ 值必须分别以十六进制字节数组的形式表示为hex(2)hex(7)类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2015-02-22
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多