V
V
vartss2019-05-01 12:29:58
PowerShell
vartss, 2019-05-01 12:29:58

How to highlight text with regular expressions in powershell?

Good day.
There is a registry file containing the path to the registry branch and the value of the certificate container.
Example

spoiler
[\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users\S-1-5-21-4126079715-2548991747-1835893097-1000\Keys\ООО Рога и Копыта]
"name.key"=hex:23,21,2F,75,73,72,2F,62,69,6E,2F,65,6E,76,20,70,79,74,68,6F,6E,0A,69,6D,70,6F,72,74,20,6F,73,0A,69,6D,70,6F,72,74,20,73,79,73,0A,0A,69,66,20,5F,5F,6E,61,6D,65,5F,5F,20,3D,3D,20,22,5F,5F,6D,61,69,6E,5F,5F,22,3A,0A,20,20,20,20,6F,73,2E,65,6E,76,69,72,6F,6E,2E,73,65,74,64,65,66,61,75,6C,74,28,22,44,4A,41,4E,47,4F,5F,53,45,54,54,49,4E,47,53,5F,4D,4F,44,55,4C,45,22,2C,20,22,64,74,70,6D,61,70,2E,73,65,74,74,69,6E,67,73,22,29,0A,20,20,20,20,74,72,79,3A,0A,20,20,20,20,20,20,20,20,66,72,6F,6D,20,64,6A,61,6E,67,6F,2E,63,6F,72,65,2E,6D
"header.key"=hex:23,21,2F,75,73,72,2F,62,69,6E,2F,65,6E,76,20,70,79,74,68,6F,6E,0A,69,6D,70,6F,72,74,20,6F,73,0A,69,6D,70,6F,72,74,20,73,79,73,0A,0A,69,66,20,5F,5F,6E,61,6D,65,5F,5F,20,3D,3D,20,22,5F,5F,6D,61,69,6E,5F,5F,22,3A,0A,20,20,20,20,6F,73,2E,65,6E,76,69,72,6F,6E,2E,73,65,74,64,65,66,61,75,6C,74,28,22,44,4A,41,4E,47,4F,5F,53,45,54,54,49,4E,47,53,5F,4D,4F,44,55,4C,45,22
[\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users\S-1-5-21-4126079715-2548991747-1835893097-1000\Keys\ООО Без Рогов и Копыт]
"name.key"=hex:23,21,2F,75,73,72,2F,62,69,6E,2F,65,6E,76,20,70,79,74,68,6F,6E,0A,69,6D,70,6F,72,74,20,6F,73,0A,69,6D,70,6F,72,74,20,73,79,73,0A,0A,69,66,20,5F,5F,6E,61,6D,65,5F,5F,20,3D,3D,20,22,5F,5F,6D,61,69,6E,5F,5F,22,3A,0A,20,20,20,20,6F,73,2E,65,6E,76,69,72,6F,6E,2E,73,65,74,64,65,66,61,75,6C,74,28,22,44,4A,41,4E,47,4F,5F,53,45,54,54,49,4E,47,53,5F,4D,4F,44,55,4C,45,22,2C,20,22,64,74,70,6D,61,70,2E,73,65,74,74,69,6E,67,73,22,29,0A,20,20,20,20,74,72,79,3A,0A,20,20,20,20,20,20,20,20,66,72,6F,6D,20,64,6A,61,6E,67,6F,2E,63,6F,72,65,2E,6D
"header.key"=hex:23,21,2F,75,73,72,2F,62,69,6E,2F,65,6E,76,20,70,79,74,68,6F,6E,0A,69,6D,70,6F,72,74,20,6F,73,0A,69,6D,70,6F,72,74,20,73,79,73,0A,0A,69,66,20,5F,5F,6E,61,6D,65,5F,5F,20,3D,3D,20,22,5F,5F,6D,61,69,6E,5F,5F,22,3A,0A,20,20,20,20,6F,73,2E,65,6E,76,69,72,6F,6E,2E,73,65,74,64,65,66,61,75,6C,74,28,22,44,4A,41,4E,47,4F,5F,53,45,54,54,49,4E,47,53,5F,4D,4F,44,55,4C,45,22,2C,20,22,64,74,70,6D,61,70,2E,73,65,74,74,69,6E,67,73,22,29,0A,20,20,20,20,74,72,79,3A,0A,20,20,20,20,20,20,20,20,66,72,6F,6D,20,64,6A,61,6E,67,6F,2E,63,6F,72,65,2E,6D,61,6E,61,67,65,6D,65,6E,74,20,69,6D,70,6F,72,74,20,65,78,65,63,75,74,65,5F,66,72,6F,6D,5F,63,6F,6D,6D,61,6E,64,5F,6C,69,6E,65,0A,20,20,20,20,65,78,63,65,70,74,20,49

С помощью powershell и регулярок мне нужно выводить значение определенной ветки реестра (имя юр. лица как переменная) (Пример, нахожу ветку [\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users\S-1-5-21-4126079715-2548991747-1835893097-1000\Keys\ООО Рога и Копыта], вывожу значение "name.key"=hex:23,21,2F,75,73,72,2F,62,69,6E,2F,65,6E.....).
В текстовом редакторе такая регулярка работает (?<=ООО Рога и Копыта])(.*\n)+?(?=\n), а вот в PS нет
$file | where {$_ -match '(?<=ООО Рога и Копыта])(.*\n)+?(?=\n)'}

В регулярках и PS опыта большого нет. Прошу помощи.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vartss, 2019-05-01
@vartss

Found a working solution in the book Effective Programming in Windows PowerShell .
(Get-Content file) -join "`n" | Select-String '(?<=OOO Horns and Hooves])(.*\n)+?(?=\n)' -all | foreach{$_.Matches}|foreach {$_.Value}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question