我正在尝试将注册表项导出到文本文件。但是,它仅导出最后一个注册表项的输出。它正在覆盖先前的输出。谁能告诉我我要去哪里错了? 预先感谢。
Function ExportRegistry ($logName)
{
$RegExportPlaceHolder = "$env:windir\Temp" + "\$logName"
if (!(Test-Path $RegExportPlaceHolder))
{
New-Item -path $RegExportPlaceHolder -type "file"
}
else
{
Add-Content $RegExportPlaceHolder $string
}
$CBSKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending"
regedit /e /y $RegExportPlaceHolder $CBSKey
$WUAUKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"
regedit /e /y $RegExportPlaceHolder $WUAUKey
$UEVKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\UpdateExeVolatile"
regedit /e /y $RegExportPlaceHolder $UEVKey
}
$RegExportLogName = "RegExport.txt"
$ExportReg = ExportRegistry $RegExportLogName
在我的情况下,我只得到$UEVKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\UpdateExeVolatile"
的输出,而先前键的输出被覆盖。
答案 0 :(得分:4)
regedit
不会追加到输出文件中。如果要将多个密钥导出到同一文件,则必须先将它们导出到单个文件,然后再将这些文件连接起来。我还建议您使用reg.exe
而不是regedit.exe
进行导出,因为只有前者会为您提供正确的退出代码,使您可以确定是否出了问题。
$CBSKey = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'
& reg export $CBSKey "${RegExportPlaceHolder}.1" /y
$WUAUKey = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
& reg export $WUAUKey "${RegExportPlaceHolder}.2" /y
$UEVKey = 'HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile'
& reg export $UEVKey "${RegExportPlaceHolder}.3" /y
'Windows Registry Editor Version 5.00' | Set-Content $RegExportPlaceHolder
Get-ChildItem "${RegExportPlaceHolder}.*" | ForEach-Object {
Get-Content $_.FullName | Select-Object -Skip 1
} | Add-Content $RegExportPlaceHolder
Remove-Item "${RegExportPlaceHolder}.*" -Force