从远程服务器上的注册表项值创建前后

时间:2016-07-23 23:53:38

标签: powershell comparison registry

为什么我之前无法获得注册表项值,我有点迷失,进行更改然后在更改后收集值以确认更改成功。

我使用以下代码获取正确的值,但未反映我对LogMaxHistoryLogLevel所做的任何更改。

我做错了吗?

foreach ($server in $servers) {
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $server)

    $RegSubKey = $Reg.OpenSubKey("SOFTWARE\\Microsoft\\CCM\\Logging\\@Global", $true)

    $LogLevelBefore      = $RegSubKey.GetValue('LogLevel')
    $LogMaxHistoryBefore = $RegSubKey.GetValue('LogMaxHistory')

    $RegSubKey = $Reg.SetValue('LogLevel', '0', [Microsoft.Win32.RegistryValueKind]::DWORD)
    $RegSubKey = $Reg.SetValue('LogMaxHistory', '6', [Microsoft.Win32.RegistryValueKind]::DWORD)

    $RegCheck = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $server)
    $RegCheck = $RegCheck.OpenSubKey("SOFTWARE\\Microsoft\\CCM\\Logging\\@Global")

    $LogLevelAfter      = $RegCheck.GetValue('LogLevel')
    $LogMaxHistoryAfter = $RegCheck.GetValue('LogMaxHistory')

    [pscustomobject]@{
        ComputerName        = $server
        LogLevelBefore      = $LogLevelBefore
        LogMaxHistoryBefore = $LogMaxHistoryBefore
        LogLevelAfter       = $LogLevelAfter
        LogMaxHistoryAfter  = $LogMaxHistoryAfter
    }
    $Reg.Close()
    $RegCheck.Close()
    GSV -ComputerName $server -Name CcmExec | Restart-Service
}

2 个答案:

答案 0 :(得分:0)

您可以使用Regquery

例如

$before=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" /ve /s

$after=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" /ve /s

write-host "registry value $before and $after" -F green

答案 1 :(得分:0)

您是否尝试使用OpenSubKey类型和RegistryKeyPermissionCheck参数重载方法RegistryKeyPermissionCheck.ReadWriteSubTree(请参阅:MSDN)?

另一个方法是使用注册表驱动器,如下所示(简化,您必须更改键和属性):

$server = '.'
Invoke-Command -ComputerName $server {
    $before = Get-ItemProperty HKLM:\SOFTWARE\PDProgs -Name Property1
    Set-ItemProperty HKLM:\SOFTWARE\PDProgs -Name Property1 -Value (Get-Random)
    $after = Get-ItemProperty HKLM:\SOFTWARE\PDProgs -Name Property1
    [psobject]@{Before = $before.Property1; After = $after.Property1}
} `
   | % { "Before = $($_.Before), After = $($_.After)" }