Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文本文件的内容正在被Powershell中以前的内容覆盖_Powershell - Fatal编程技术网

文本文件的内容正在被Powershell中以前的内容覆盖

文本文件的内容正在被Powershell中以前的内容覆盖,powershell,Powershell,我正在尝试将注册表项导出到文本文件。但是,它只是导出最后一个注册表项的输出。它正在覆盖以前的输出。谁能告诉我哪里出了问题? 提前谢谢 Function ExportRegistry ($logName) { $RegExportPlaceHolder = "$env:windir\Temp" + "\$logName" if (!(Test-Path $RegExportPlaceHolder)) { New-Item -path $RegExportPl

我正在尝试将注册表项导出到文本文件。但是,它只是导出最后一个注册表项的输出。它正在覆盖以前的输出。谁能告诉我哪里出了问题? 提前谢谢

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\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Update\UpdateExeVolatile”
的输出,以前的键的输出被覆盖。

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

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

非常感谢你的帮助。如果注册表项不存在,则我将收到以下错误:reg.exe:错误:系统无法找到指定的注册表项或值。当然是这样。这就是使用
reg.exe
的基本要点:这样,当出现问题时,您就可以真正看到发生了什么。您可以通过如下重定向来抑制错误输出:
®;导出。。。2> $null
。为什么脚本要创建多个文件?是,理解。我们如何使用®;导出准确地抑制错误。。。2> 零美元?它发出重定向错误。请显示您正在运行的确切命令和确切的错误消息。非常感谢您的帮助。如果注册表项不存在,则我将收到以下错误:reg.exe:错误:系统无法找到指定的注册表项或值。当然是这样。这就是使用
reg.exe
的基本要点:这样,当出现问题时,您就可以真正看到发生了什么。您可以通过如下重定向来抑制错误输出:
®;导出。。。2> $null
。为什么脚本要创建多个文件?是,理解。我们如何使用®;导出准确地抑制错误。。。2> 零美元?它发出重定向错误。请显示您正在运行的确切命令和确切的错误消息。