list - 在 Powershell 中编辑注册表值的多字符串数组

标签 list powershell registry

如何使用 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/

相关文章:

Python 列表 : Is this the best way to remove duplicates while preserving order?

c# - 以初始化 List<T> 的方式初始化 ObservableCollection<T> 的长度

windows - 如何在 Powershell 中执行 head、tail、more、less、sed 的操作?

winapi - 哪些原因会导致 ShellExecute 失败?

c - RegEnumValue 的错误代码 87

html - 居中下拉列表

c# - 将对象列表绑定(bind)到图表

.net - 检查远程 session

Powershell 将 String[] 转换为 List<String>

winapi - Windows XP、HKCU\...\LastVisitedMRU 允许的最大项目数?