加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何使用PowerShell获取键内注册表值的数据值

发布时间:2020-12-14 05:38:57 所属栏目:Windows 来源:网络整理
导读:在我的函数中,我想在特定子项的数据部分中搜索特定字符串.函数内部的一部分代码如下: foreach ($CurrentPath in $Path) { $Items = Get-Item -Path Registry::$CurrentPath ForEach ( $Property in $Items) { $Key = $Property 当我在$Key = $Property之后
在我的函数中,我想在特定子项的数据部分中搜索特定字符串.函数内部的一部分代码如下:

foreach ($CurrentPath in $Path) { 
    $Items = Get-Item -Path Registry::$CurrentPath
    ForEach ( $Property in $Items) {
      $Key = $Property

当我在$Key = $Property之后直接调试并转到主机命令窗口并输入$Key并按Enter键.它返回:

Hive: hkcu

Name                           Property
----                           --------
ABC                            Test : abababMSSQLSERVER_2014ababab

我希望我的功能还可以搜索数据部分,如附在图像中所示,这是在regedit中.

Regedit image sample

我如何完成搜索?

解决方法

如果您不知道值名称,则需要循环属性.使用Get-Item的示例:

$item = Get-Item -Path "Registry::HKEY_CURRENT_USERSoftwareNuGet"

foreach ($prop in $item.Property) {
    if($item.GetValue($prop) -match '0') { "Match found in key $($item.PSPath),value $($prop)" }
}

或者Get-ItemProperty:

$item = Get-ItemProperty -Path "Registry::HKEY_CURRENT_USERSoftwareNuGet"

foreach ($prop in $item.psobject.Properties) {
    if($prop.Value -match '0') { "Match found in key $($item.PSPath),value $($prop.Name)" }   
}

如果您知道value-name,那么使用Get-ItemProperty和Where-Object更容易,例如:

Get-ItemProperty -Path "Registry::HKEY_CURRENT_USERSoftwareNuGet" | Where-Object { $_.IncludePrerelease -match '0' }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读