0

我有 2 个文本文件,一个包含远程服务器的注册表设置列表,另一个包含首选注册表设置。这些文件的格式相同,即:

ServValues.txt

HKLM:\software\Wow6432Node\Policies\Citrix\, Evidence, DesktopKind, Private
HKLM:\software\Wow6432Node\Policies\Citrix\, MultimediaPolicies, EnableRAVE, 1
HKLM:\software\Wow6432Node\Policies\Citrix\, ICAPolicies, ACRLogEvents, 1
HKLM:\software\Wow6432Node\Policies\Citrix\, ICAPolicies, ACRRequireAuth, 0

正确值.txt

HKLM:\software\Wow6432Node\Policies\Citrix\, Evidence, DesktopKind, Private
HKLM:\software\Wow6432Node\Policies\Citrix\, MultimediaPolicies, EnableRAVE, 1
HKLM:\software\Wow6432Node\Policies\Citrix\, ICAPolicies, ACRLogEvents, 0
HKLM:\software\Wow6432Node\Policies\Citrix\, ICAPolicies, ACRRequireAuth, 2

我正在尝试编写一个 powershel 脚本来比较这两个文件,并在发现差异(如第三行和第四行)时创建一个新的日志文件,如:

差异.txt

RemoteSever >HKLM:\software\Wow6432Node\Policies\Citrix\,ICAPolicies,ACRLogEvents 设置为 1,应为 0

RemoteServer >>HKLM:\software\Wow6432Node\Policies\Citrix\,ICAPolicies,ACRRequireAuth 设置为 0,应为 2

我只比较任何行中最后一个逗号之后的值,其余的假定是相同的。我被难住了,有什么建议吗?

4

1 回答 1

1

你试过比较对象吗?

如果这不起作用,您将不得不使用标准for循环:

filter Split-RegSettingLine
{
    param(
        [Parameter(ValueFromPipeline=$true)]
        $Line
    )
    if( $Line -notmatch '^(.*), ([^,]+)$' )
    {
        Write-Error ('Invalid line: {0}' -f $Line)
        return
    }
    $matches[1],$matches[2]
}

$ServerSettings = Get-Content ServValues.txt
$CorrectSettings = Get-Content CorrectValues.txt

if( $ServerSettings.Length -ne $CorrectSettings.Length )
{
    Write-Warning ('Files have different number of lines.  WATCH OUT!')
}

for( $idx = 0 $idx -lt $ServerSettings.Length -and $idx -lt $CorrectSettings.Length; ++$idx )
{
    $serverKey,$serverValue = $ServerSettings[$idx] | Split-RegSettingLine
    $correctKey,$correctValue = $CorrectSettings[$idx] | Split-RegSettingLine

    if( $serverKey -ne $correctKey )
    {
        Write-Error ('Line {0}: mismatched keys: <{1}> <=> <{2}>' -f $idx,$serverKey,$correctKey)
        continue
    }

    if( $serverValue -ne $correctValue )
    {
        Write-Error ('Line {0}: key {1}: set to <{2}>, should be <{3}>' -f $idx,$serverKey,$serverValue,$correctValue
    }
}
于 2013-07-04T19:40:55.387 回答