在我的函数中,我想在特定子项的数据部分中搜索特定字符串。函数内部的一部分代码如下:
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 : ababab\MSSQLSERVER_2014\ababab
我希望我的功能也可以搜索数据部分,如附在图片中所示,这是在注册表中。
如何完成搜索?
答案 0 :(得分:1)
如果您不知道值名称,则需要循环属性。使用Get-Item
的示例:
$item = Get-Item -Path "Registry::HKEY_CURRENT_USER\Software\NuGet"
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_USER\Software\NuGet"
foreach ($prop in $item.psobject.Properties) {
if($prop.Value -match '0') { "Match found in key $($item.PSPath) , value $($prop.Name)" }
}
如果你知道价值名称,那么使用Get-ItemProperty
和Where-Object
就更容易,例如:
Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\NuGet" | Where-Object { $_.IncludePrerelease -match '0' }