如何使用Powershell 2.0创建新的MultiString阵列并将其远程传递到注册表?
#get the MultiLine String Array from the registry
$regArry = (Get-Itemproperty "hklm:\System\CurrentControlSet\Control\LSA" -name "Notification Packages").("Notification Packages")
#Create a new String Array
[String[]]$tempArry = @()
#Create an ArrayList from the Registry Array so I can edit it
$tempArryList = New-Object System.Collections.Arraylist(,$regArry)
# remove an entry from the list
if ( $tempArryList -contains "EnPasFlt" )
{
$tempArryList.Remove("EnPasFlt")
}
# Add an entry
if ( !($tempArryList -contains "EnPasFltV2x64"))
{
$tempArryList.Add("EnPasFltV2x64")
}
# Convert the list back to a multi-line Array It is NOT creating new Lines!!!
foreach($i in $tempArryList) {$tempArry += $1 = "\r\n"]}
# Remove the old Array from the Registry
(Remove-ItemProperty "hklm:\System\CurrentControlSet\Control\Lsa" -name "notification packages").("Notification Packages")
# Add the new one
New-itemproperty "hklm:\System\CurrentControlSet\Control\Lsa" -name "notification packages" -PropertyType MultiString -Value "$tempArry"
一切都很好,除了我不能让值到一个新的行。我试过了/r/n
和'r'n
。我在注册表中的输出显示一行上的所有内容,并添加我添加的文字换行符和回车符号。我如何让Array识别这些而不是字面打印它们?
答案 0 :(得分:3)
没有必要摆弄ArrayList
和换行符。特别是如果要修改远程注册表。只需使用Microsoft.Win32.RegistryKey
类:
$server = '...'
$subkey = 'SYSTEM\CurrentControlSet\Control\LSA'
$value = 'Notification Packages'
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
$key = $reg.OpenSubKey($subkey, $true)
$arr = $key.GetValue($value)
$arr = @($arr | ? { $_ -ne 'EnPasFlt' })
if ($arr -notcontains 'EnPasFltV2x64') {
$arr += 'EnPasFltV2x64'
}
$key.SetValue($value, [string[]]$arr, 'MultiString')
答案 1 :(得分:2)
在Powershell中,转义字符是一个反引号“`”,而不是撇号'
。所以你想试试这个:
foreach($i in $tempArryList) {$tempArry += $1 = "`r`n"]}
可能选择了Backtick,因为反斜杠\
是Windows中的路径分隔符。