在 Powershell 中为注册表值编辑多字符串数组

ant*_*n1p 2 registry powershell list

如何使用 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"
Run Code Online (Sandbox Code Playgroud)

一切都很好,只是我无法将值转到新行。我试过/r/n'r'n。我在注册表中的输出将所有内容都显示在一行上,并添加了我添加的文字换行符和回车标志。我如何让数组识别这些而不是字面上打印它们?

Ans*_*ers 5

没有必要摆弄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')
Run Code Online (Sandbox Code Playgroud)