首页 > 解决方案 > 脚本作为本地系统运行时的额外字符

问题描述

我有一个包含几个功能的脚本。第一个函数在 HKLM\Software\Test (\UDF1-30) 中创建一些注册表项。第二个函数获取写在 UDF# 键中的任何字符串,包含它们(每个 UDF 一行,由管道字符分隔)并将它们复制到 HKLM\Software\CentraStage\Custom#。

为了测试,我将以下字符串放入 UDF12:

PatchSched:{"StartTime":"23:00:00","TzBias":-480,"Duration":240,"DayOfYear":[],"DayOfWeek":[-2],"Days":[],"MonthlyDayOfWeek":[6],"Months":[1,2,3,4,5,6,7,8,9,10,11,12],"Ordering":[4],"ScheduleType":7}

当我以本地管理员身份运行脚本时,我将相同的字符串输入 Custom12。但是,当我将脚本作为本地系统运行时,我会在字符串中得到随机管道:

PatchSched:{"StartTime":"23:00:00","TzBias":-480,"Duration":240,"DayOfYear":[],"DayOfWeek":[-2],"Days":[],"MonthlyD|ayOfWeek":[6],"Months":[1,2,3,4,5,6,7,8,9,10,11,12],"Ordering":[4],"ScheduleType":7}

世界上,为什么会发生这种事?这是脚本:

Function Add-UserDefinedFields {
    <#
        .DESCRIPTION
            This function checks if HKLM\SOFTWARE\Test exists. If not, it creates the required registry structure, to support Update-UserDefinedFields.
    #>

    Set-Location HKLM:

    If (-Not(Test-Path .\Software\Test\UDF29)) {
        # If the Test registry key does not exist...
        # Create the Test registry key.
        New-Item -Path .\Software -Name Test

        # Create 30 UDF registry keys.
        For ($i = 1; $i -le 30; $i++) {
            New-Item -Path .\Software\Test -Name UDF$i
        }
    }
}

Function Update-UserDefinedFields {
    <#
        .DESCRIPTION
            This function reads the value of each UDF registry entry, in HKLM\SOFTWARE\Test and writes the value(s) to the corresponding UDF in HKLM\SOFTWARE\CentraStage.
    #>

    Set-Location HKLM:

    For ($i = 1; $i -le 30; $i++) {
        # For each of the 30 UDF registry keys...

        # Initialize variable.
        $udfValue = New-Object "System.Collections.Generic.List[string]"

        Get-ItemProperty .\SOFTWARE\Test\UDF$i -ErrorAction SilentlyContinue | Out-String -Stream | Where-Object { $_ -NOTMATCH '^ps.+' } | ForEach-Object {
            $udfValue.Add($_)
        }

        $udfString = $udfValue -join '|'

        $udfString = $udfString.Replace(' ', '')
        While ($udfString -like "*||*") {
            $udfString = $udfString.replace('||', '|')
        }

        If ($udfString) {
            # Trim the leading and trailing characters (|).
            $udfString = $udfString.substring(1, $udfString.length - 2)
        }

        Write-Host ("Writing to UDF{0}: {1}" -f $i, $udfString)

        # For each Test UDF, write the concatinated value to the corresponding AEM UDF registry location.
        $null = New-ItemProperty -Path .\SOFTWARE\CentraStage -Name Custom$i -PropertyType String -Value $udfstring -Force -ErrorAction SilentlyContinue
    }
}

Add-UserDefinedFields
Update-UserDefinedFields

标签: powershellregistry

解决方案


好吧,我想通了。代码现在如下所示:

    Function Add-TestUserDefinedFields {
    <#
        .DESCRIPTION
            This function checks if HKLM\SOFTWARE\Test exists. If not, it creates the required registry structure, to support Update-UserDefinedFields.
    #>

    Set-Location HKLM:

    If (-Not(Test-Path .\Software\Test\UDF29)) {
        # If the Test registry key does not exist...
        # Create the Test registry key.
        New-Item -Path .\Software -Name Test

        # Create 30 UDF registry keys.
        For ($i = 1; $i -le 30; $i++) {
            New-Item -Path .\Software\Test -Name UDF$i
        }
    }
}

Function Update-UserDefinedFields {
    <#
        .DESCRIPTION
            This function reads the value of each UDF registry entry, in HKLM\SOFTWARE\Test and writes the value(s) to the corresponding UDF in HKLM\SOFTWARE\CentraStage.
    #>

    Set-Location HKLM:

    For ($i = 1; $i -le 30; $i++) {
        # For each of the 30 UDF registry keys...

        # Initialize variable.
        $udfValue = New-Object "System.Collections.Generic.List[string]"

        (Get-ItemProperty .\SOFTWARE\Test\UDF$i -ErrorAction SilentlyContinue).PSObject.Properties | Where-Object { $_.Name -NOTMATCH '^ps.+' } | ForEach-Object {
            $udfValue.Add("$($_.Name):$($_.Value)")
        }

        $udfString = $udfValue -join '|'

        Write-Output ("The value of `$udfString is {0}" -f $udfString) | out-file C:\Synoptek\test.txt -Append

        $udfString = $udfString.Replace(' ', '')
        While ($udfString -like "*||*") {
            $udfString = $udfString.replace('||', '|')
        }

        Write-Output ("Writing to UDF{0}: {1}" -f $i, $udfString)

        # For each Test UDF, write the concatinated value to the corresponding  UDF registry location.
        $null = New-ItemProperty -Path .\SOFTWARE\CentraStage -Name Custom$i -PropertyType String -Value $udfstring -Force -ErrorAction SilentlyContinue
    }
}

Add-TestUserDefinedFields
Update-UserDefinedFields

推荐阅读