如何使用 Powershell 2.0 创建一个新的多字符串数组并将其远程传递到注册表?
#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
。我在注册表中的输出在一行中显示了所有内容,并添加了我添加的文字换行符和回车标志。我如何让阵列识别这些而不是字面上打印它们?
最佳答案
无需摆弄 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')
关于list - 在 Powershell 中编辑注册表值的多字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27238523/