2017-08-16 64 views
0

我正在尋找基於其子項內找到的值更改註冊表值。請看下面的圖。註冊表 - 搜索子值以更改父項值

- 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

回答

0

一旦您完成了父項的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 
    } 
}