使用 PowerShell 查询单个注册表子项? [英] Query a single registry subkey using PowerShell?

查看:65
本文介绍了使用 PowerShell 查询单个注册表子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本,它将搜索以下注册表值HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations,如果存在,请将其删除.

I have created a script which will search for the following registry value HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperationsand if it is present, delete it.

但是,我正在努力查询要删除的单个值,而不是 ..\Session Manager\ 中的所有值.我有以下代码:

However, I am struggling to query the single value I want to delete rather than all values within ..\Session Manager\. I have the following code:

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $computer)
$regKey = $reg.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\",$true )

foreach ($key in $regKey.GetValueNames()) {

if ($key -eq "PendingFileRenameOperations")
  {
    $regKey.DeleteValue($key)
    Write-Host "PendingFileRenameOperations key deleted successfully" -ForegroundColor Green
  }
else
  {
    Write-Host "Key does not exist. Please assign to Second Line for further investigation" -ForegroundColor Red
  }
}

输出以下内容:

Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
Key does not exist. No further action required
PendingFileRenameOperations key deleted successfully

是否可以只输出 Key 不存在.不需要进一步的操作PendingFileRenameOperations 键删除成功 而不是查询每个值?

Is it possible to just output either Key does not exist. No further action required or PendingFileRenameOperations key deleted successfully rather than querying every single value?

推荐答案

阐述我的评论:

$ErrorActionPreference = 'SilentlyContinue'
$Path = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
$Name = 'PendingFileRenameOperations'
If (Get-ItemProperty -Path $Path -Name $Name)
{
    Remove-ItemProperty -Path $Path -Name $Name
    "Property '$Name' removed."
}
Else
{
    "Property '$Name' did not exist."
}

<小时>

对于您的具体问题,创建一个标志变量:


For your specific question, create a flag variable:

$Flag = $True
foreach ($property in $regKey.GetValueNames())
{
  if ($property -eq 'PendingFileRenameOperations')
  {
    $regKey.DeleteValue($property)
    Write-Host "PendingFileRenameOperations key deleted successfully" -ForegroundColor Green
    $Flag = $False
    break
  }
}
if ($Flag)
{
  Write-Host 'Property does not exist. Please assign to Second Line for further investigation' -ForegroundColor Red
}

<小时>

关于@EBGreen 的建议:


And with respect to @EBGreen's suggestion:

If ($regKey.GetValue('PendingFileRenameOperations'))
{
    $regKey.DeleteValue('PendingFileRenameOperations')
    Write-Host 'PendingFileRenameOperations property deleted successfully' -ForegroundColor Green
}
Else
{
    Write-Host 'Property does not exist. Please assign to Second Line for further investigation' -ForegroundColor Red
}

这篇关于使用 PowerShell 查询单个注册表子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆