[英]How to select the first matching line after an already matched line in powershell?
我正在嘗試使用powershell從powercfg
的輸出查詢一些Windows電源設置。 我有足夠的信息來將范圍縮小到一組子設置,但是在文本塊中,我仍然需要使用GUID查找匹配的設置,然后需要提取該設置的當前設置值。 我能夠使用Select-String -Context
實現此Select-String -Context
,但是它不是動態的,因此容易出錯。 我正在尋找一種更干凈的方法來提取價值。
這是我擁有的文本塊的一個示例(存儲在$block
):
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
GUID Alias: SCHEME_BALANCED
Subgroup GUID: 238c9fa8-0aad-41ed-83f4-97be242c8f20 (Sleep)
GUID Alias: SUB_SLEEP
Power Setting GUID: 29f6c1db-86da-48c5-9fdb-f2b67b1f44da (Sleep after)
GUID Alias: STANDBYIDLE
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00000708
Current DC Power Setting Index: 0x00000384
Power Setting GUID: 94ac6d29-73ce-41a6-809f-6363ba21b47e (Allow hybrid sleep)
GUID Alias: HYBRIDSLEEP
Possible Setting Index: 000
Possible Setting Friendly Name: Off
Possible Setting Index: 001
Possible Setting Friendly Name: On
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: 9d7815a6-7ee4-497e-8888-515a05f02364 (Hibernate after)
GUID Alias: HIBERNATEIDLE
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00002a30
Current DC Power Setting Index: 0x00002a30
Power Setting GUID: bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d (Allow wake timers)
GUID Alias: RTCWAKE
Possible Setting Index: 000
Possible Setting Friendly Name: Disable
Possible Setting Index: 001
Possible Setting Friendly Name: Enable
Possible Setting Index: 002
Possible Setting Friendly Name: Important Wake Timers Only
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
假設我要提取“ Allow hybrid sleep
的AC值,在這種情況下為0x00000001
。 我可以將setting_guid
插入查詢字符串中。
現在,我正在使用這段代碼來動態提取特定設置的值:
$block = powercfg /q #{scheme_guid} #{sub_guid}
$setting = $block | Select-String -Pattern #{setting_guid} -Context 0, 8 | %{$_.Context.PostContext}
$line = $setting | Select-String -Pattern 'Current #{ac_or_dc} Power Setting Index'
$line -match 'Current #{ac_or_dc} Power Setting Index: (?<value>0x.{8})'
Write-Output ([int]$matches['value'])
-Context 0, 8
可以正常工作,但是硬編碼的-Context 0, 8
並不是真正可取的,因為有時塊可能真的很短或很長,而我的查詢將無法提取值或從錯誤的行中檢索。 我希望找到一種更清潔的方式來做到這一點,最好是程序化且易於閱讀(只要有意義就可以使用正則表達式 )。
如果我們希望使用正則表達式執行此任務,則可能需要以傳遞換行符的表達式開始,類似於以下內容:
Allow hybrid sleep[\s\S]+?Current DC Power Setting Index:\s+(.+)\s
Allow hybrid sleep[\s\S]+?Current DC Power Setting Index:\s+(.+)
Allow hybrid sleep[\w\W]+?Current DC Power Setting Index:\s+(.+)\s
Allow hybrid sleep[\d\D]+?Current DC Power Setting Index:\s+(.+)
我們期望的輸出將在此捕獲組中: (.+)
。
如果不需要此表達式,並且希望對其進行修改,請訪問此鏈接regex101.com 。
jex.im可視化正則表達式:
這樣吧...
$PowerData = @'
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
GUID Alias: SCHEME_BALANCED
Subgroup GUID: 238c9fa8-0aad-41ed-83f4-97be242c8f20 (Sleep)
GUID Alias: SUB_SLEEP
Power Setting GUID: 29f6c1db-86da-48c5-9fdb-f2b67b1f44da (Sleep after)
GUID Alias: STANDBYIDLE
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00000708
Current DC Power Setting Index: 0x00000384
Power Setting GUID: 94ac6d29-73ce-41a6-809f-6363ba21b47e (Allow hybrid sleep)
GUID Alias: HYBRIDSLEEP
Possible Setting Index: 000
Possible Setting Friendly Name: Off
Possible Setting Index: 001
Possible Setting Friendly Name: On
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: 9d7815a6-7ee4-497e-8888-515a05f02364 (Hibernate after)
GUID Alias: HIBERNATEIDLE
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00002a30
Current DC Power Setting Index: 0x00002a30
Power Setting GUID: bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d (Allow wake timers)
GUID Alias: RTCWAKE
Possible Setting Index: 000
Possible Setting Friendly Name: Disable
Possible Setting Index: 001
Possible Setting Friendly Name: Enable
Possible Setting Index: 002
Possible Setting Friendly Name: Important Wake Timers Only
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
'@
[regex]::Matches($PowerData,'(?s)hybrid.*?DC').Value |
ForEach {[regex]::Matches($PSitem,'Current AC Power Setting Index.*').Value}
# Results
Current AC Power Setting Index: 0x00000001
要么
Clear-Host; (((Get-Content -Path '.\PowerDataTemplate.txt')) -match 'hybrid|AC')#[3]
<#
Current AC Power Setting Index: 0x00000708
Power Setting GUID: 94ac6d29-73ce-41a6-809f-6363ba21b47e (Allow hybrid sleep)
GUID Alias: HYBRIDSLEEP
Current AC Power Setting Index: 0x00000001
Current AC Power Setting Index: 0x00002a30
Power Setting GUID: bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d (Allow wake timers)
Current AC Power Setting Index: 0x00000001
#>
Clear-Host; (((Get-Content -Path '.\PowerDataTemplate.txt')) -match 'hybrid|AC')[3]
# Current AC Power Setting Index: 0x00000001
Clear-Host; [regex]::matches($(Get-Content -Path 'variable:\PowerData'),'.*hybrid.*|.*AC.*').Value
<#
Current AC Power Setting Index: 0x00000708
Power Setting GUID: 94ac6d29-73ce-41a6-809f-6363ba21b47e (Allow hybrid sleep)
Current AC Power Setting Index: 0x00000001
Current AC Power Setting Index: 0x00002a30
Current AC Power Setting Index: 0x00000001
#>
Clear-Host; [regex]::matches($(Get-Content -Path 'variable:\PowerData'),'.*hybrid.*|.*AC.*').Value[2]
# Current AC Power Setting Index: 0x00000001
或者在PowerShellv5x中,可以使用具有字符串轉換模板的ConvertFrom-String或ConvertFrom-StringData cmdlet。
更新OP
我對此進行了更多介紹,如果您所追求的只是一個特定的設置,那為什么不直接要求它呢。 例如。
# Get only (Allow hybrid sleep)
powercfg.exe query SCHEME_MIN SUB_SLEEP HYBRIDSLEEP
# Get only the AC setting
(powercfg.exe query SCHEME_MIN SUB_SLEEP HYBRIDSLEEP) -match 'Current AC Power Setting Index'
# Results
Current AC Power Setting Index: 0x00000000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.