注册表 - 搜索子值以更改父项值

问题描述:

我正在寻找基于其子项内找到的值更改注册表值。请看下面的图。注册表 - 搜索子值以更改父项值

- scripts 
    - {ID} 
    + ScriptState = 0 #This is the value I am looking to change 
    - properties 
     + {ID},E = 'Activated' #based on the values of these registry values 
     + {ID},V = 'Hard Drive' #based on the values of these registry values 

注:

+ = Value - = Key

所有的ID是随机产生的,和我有麻烦通过循环/获得随机产生的密钥ID列表。一旦我能够遍历这些关键ID,那么其余的应该相当容易。

下面是我正在使用的当前脚本,试图找到并过滤子键(改变父母的父键值尚未实现)。

V1:

Get-ChildItem -Path $key -rec | foreach { 
    Get-ChildItem -Path $_.PSPath -rec } | foreach { 
    $CurrentKey = (Get-ItemProperty -Path $_.PsPath) } | 
    select-string "REGEX TO FIND VALUES" -input $CurrentKey -AllMatches | 
    foreach {($_.matches)| select-object Value 
} 

V2:

Get-ChildItem -Path $key -rec | foreach { 
    Get-ChildItem -Path $_.PSPath -rec | foreach { 
    $CurrentKey = (Get-ItemProperty -Path $_.PsPath) 
    if ($CurrentKey -match "REGEX TO FIND VALUES") { 
     $CurrentKey 
    } 
}} 

无论上面的脚本产生任何结果,我希望有人可以帮我明白为什么他们不这样做,或提供/指向我将实现这一目标的代码。


P.S.对于这个无意义的标题感到抱歉,很难用几句话来形容这一点。

+0

那么,什么是问题? :-) –

+0

道歉,我忽略了最重要的部分。我现在用一个问题更新了问题... – Kickball

一旦您完成了父项的Get-ChildItem -Recurse,就不需要对ChildItem执行额外的-Recurse。所有的ChildItems已经在内存中,并准备好在你的管道中使用。我不确定下面的内容是否会有所帮助,但这是我如何去寻找键值列表中的某个值。

$DebugPreference = 'Continue' 
#Any Get-ChildItem with -Recurse will get all items underneath it, both childitems and their childitems. 
$regKeySet = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Recurse 
foreach($childKey in $regKeySet) 
{ 
    Write-Debug -Message "Processing registry key $($childKey.Name)" 
    #You can pick any property you want, the -ErrorAction is set here to SilentlyContinue to cover the instances 
    #where the specific childitem does not contain the property you are looking for, the errors are typically non-terminating but it cleans up the red. 
    $publisherInfo = Get-ItemProperty $childKey.Name -Name Publisher -ErrorAction SilentlyContinue 

    if($publisherInfo.Publisher -ieq 'Microsoft Corporation') 
    { 
     #Do stuff here, you mention doing something to the parent, this is easily accomplished by 
     #just referecning the $childKey that is in this loop. If the publisher equals something you can then manipulate any property of the parent you would like. 
     Write-Host "Found the publisher I wanted: $($publisherInfo.Publisher)." -ForegroundColor Green 
    } 
}