我有一个带有几个功能的脚本。第一个功能在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
答案 0 :(得分:0)
好的,我知道了。现在的代码如下所示:
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