批处理之家's Archiver

5i365 发表于 2022-4-23 12:45

PowerShell提取命令帮助文件中的示例

我想将某个命令帮助文件中的示例提取出来, 期待高手能支招, 提前感谢!
最下面的代码, 只提取出了示例的标题, 标题下有几行是示例, 很有规律, [b][color=#ff0000]正则提取:[/color][/b] 标题行下有一个空行, 然后就是1行或多行连续的示例, 接着有一个空行

例如 [color=#ff0000][b]get-Content[/b][/color] 这个命令帮助的示例, 我节选了一些, 其中的蓝字部分就是示例


   ------------------ Example 6: Get raw content ------------------

[color=#0000ff]    $raw = Get-Content -Path .\LineNumbers.txt -Raw[/color]
[color=#0000ff]    $lines = Get-Content -Path .\LineNumbers.txt[/color]
[color=#0000ff]    Write-Host "Raw contains $($raw.Count) lines."[/color]
[color=#0000ff]    Write-Host "Lines contains $($lines.Count) lines."[/color]

    Raw contains 1 lines.
    Lines contains 100 lines.


    ----------- Example 7: Use Filters with Get-Content -----------

[color=#ff0000] [/color][color=#0000ff]   Get-Content -Path C:\Temp\* -Filter *.log[/color]


    --------- Example 8: Get file contents as a byte array ---------

[color=#0000ff]    $byteArray = Get-Content -Path C:\temp\test.txt -Encoding Byte -Raw[/color]
[color=#0000ff]    Get-Member -InputObject $bytearray[/color]

    TypeName: System.Byte[]

    Name           MemberType            Definition
    ----           ----------            ----------
    Count          AliasProperty         Count = Length
    Add            Method                int IList.Add(System.Object value)

----------------------------------------------------------------------------------------------------------------------------
[b][color=#ff0000]期待提取后的结果:[/color][/b]

   ------------------ Example 6: Get raw content ------------------

[color=#0000ff]    $raw = Get-Content -Path .\LineNumbers.txt -Raw[/color]
[color=#0000ff]    $lines = Get-Content -Path .\LineNumbers.txt[/color]
[color=#0000ff]    Write-Host "Raw contains $($raw.Count) lines."[/color]
[color=#0000ff]    Write-Host "Lines contains $($lines.Count) lines."[/color]

    ----------- Example 7: Use Filters with Get-Content -----------

[color=#ff0000] [/color][color=#0000ff]   Get-Content -Path C:\Temp\* -Filter *.log[/color]

    --------- Example 8: Get file contents as a byte array ---------

[color=#0000ff]    $byteArray = Get-Content -Path C:\temp\test.txt -Encoding Byte -Raw[/color]
[color=#0000ff]    Get-Member -InputObject $bytearray[/color]

[color=#0000ff]
[/color]
----------------------------------------------------------------------------------------------------------------------------[code]$c = "get-Content"
((get-help $c -Examples | Out-String) -split '\n') | foreach { $_.trim() } | sls ".+Example\s\d:.+"[/code]

idwma 发表于 2022-4-23 14:57

[code]$c = "get-Content"
((get-help $c -Examples | Out-String) -split '\n') | foreach { $_.trim() } | sls ".+Example\s\d:.+|$c|\$"[/code]

5i365 发表于 2022-4-23 16:23

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254739&ptid=62461]2#[/url] [i]idwma[/i] [/b]


   感谢大侠支招, 匹配还是有点问题, 结果如下:
Get-Content
---------- Example 1: Get the content of a text file ----------
1..100 | ForEach-Object { Add-Content -Path .\LineNumbers.txt -Value "This is line $_." }
Get-Content -Path .\LineNumbers.txt
ate the `LineNumbers.txt` file. The variable `$_` represents the array values as each object is sent down the pipeline. The `Get-Content` cmdlet uses the P
--- Example 2: Limit the number of lines Get-Content returns ---
Get-Content -Path .\LineNumbers.txt -TotalCount 5
-- Example 3: Get a specific line of content from a text file --
(Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]
The `Get-Content` command is wrapped in parentheses so that the command completes before going to the next step. `Get-Content`returns an array of lines, th
--------- Example 4: Get the last line of a text file ---------
Get-Item -Path .\LineNumbers.txt | Get-Content -Tail 1
This example uses the `Get-Item` cmdlet to demonstrate that you can pipe files into the `Get-Content` parameter. The Tail parameter gets the last line of t
---- Example 5: Get the content of an alternate data stream ----
PSPath        : Microsoft.PowerShell.Core\FileSystem::C:\Test\Stream.txt::$DATA
PSChildName   : Stream.txt::$DATA
Stream        : :$DATA
# Retrieve the content of the primary, or $DATA stream.
Get-Content -Path .\Stream.txt -Stream $DATA
PSPath        : Microsoft.PowerShell.Core\FileSystem::C:\Test\Stream.txt::$DATA
PSChildName   : Stream.txt::$DATA
Stream        : :$DATA
Get-Content -Path .\Stream.txt -Stream NewStream
). By default `Get-Content` only retrieves data from the primary, or `$DATA` stream. Streams can be used to store hidden data such as attributes, security
------------------ Example 6: Get raw content ------------------
$raw = Get-Content -Path .\LineNumbers.txt -Raw
$lines = Get-Content -Path .\LineNumbers.txt
Write-Host "Raw contains $($raw.Count) lines."
Write-Host "Lines contains $($lines.Count) lines."
----------- Example 7: Use Filters with Get-Content -----------
Get-Content -Path C:\Temp\* -Filter *.log
--------- Example 8: Get file contents as a byte array ---------
$byteArray = Get-Content -Path C:\temp\test.txt -Encoding Byte -Raw
Get-Member -InputObject $bytearray

idwma 发表于 2022-4-23 18:23

[code]$c = "get-Content"
(get-help $c -Examples | Out-String) -replace '[\s\S]*?(.+Example\s\d:.+\n\s*\n[\s\S]*?\n\s*\n)[\s\S]*?\n\s*\n\s*\n','$1'[/code]

5i365 发表于 2022-4-23 18:29

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254755&ptid=62461]4#[/url] [i]idwma[/i] [/b]


   有几个示例丢了

    ---------- Example 1: Get the content of a text file ----------

    1..100 | ForEach-Object { Add-Content -Path .\LineNumbers.txt -Value "This is line $_." }
    Get-Content -Path .\LineNumbers.txt

    -- Example 3: Get a specific line of content from a text file --

    (Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]

    ----------- Example 7: Use Filters with Get-Content -----------

    Get-Content -Path C:\Temp\* -Filter *.log

idwma 发表于 2022-4-23 18:34

ps版本不一样输出的格式不一样,发个完整的help gc输出来看看

5i365 发表于 2022-4-23 18:37

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254757&ptid=62461]6#[/url] [i]idwma[/i] [/b]

哦, 我的是WIN7下的帮助

   [url]https://send.cm/d/AM0K[/url]

idwma 发表于 2022-4-23 19:07

[code]$c = "get-Content"
[regex]::matches($(get-help $c -Examples | Out-String),'(.+Example\s\d:.+\n\s*\n[\s\S]*?)\n\s*\n')|%{$_.groups[1].value}[/code]

5i365 发表于 2022-4-23 19:12

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254763&ptid=62461]8#[/url] [i]idwma[/i] [/b]


感谢大侠帮忙,
看来正则配合分组还是很有用, 很常见的, 多个示例, 都用到了group

5i365 发表于 2022-4-23 19:15

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254763&ptid=62461]8#[/url] [i]idwma[/i] [/b]


大侠, 总感觉, 用  select-string 能更好的解决问题,  因为它后面可以加, 多个模式匹配的字符串
Select-String [-Pattern] <String[]>

idwma 发表于 2022-4-23 22:08

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254765&ptid=62461]10#[/url] [i]5i365[/i] [/b]


    用这个怎么判断多行?

5i365 发表于 2022-4-23 22:17

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254771&ptid=62461]11#[/url] [i]idwma[/i] [/b]


  它 有一个  上下文 参数, 选择匹配前扣的行, 可能用不上,
最主要是可以同时用多个正则参数, 配合一下, 好像无所不能似的, 我对正则, 迷迷糊糊的, 一配合就更晕了 :lol

这个选择字符串很强大, 看到好多国外的贴子, 截取段落什么的, 都用的它, 很少用match什么的

idwma 发表于 2022-4-23 22:21

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254773&ptid=62461]12#[/url] [i]5i365[/i] [/b]


    那些功能好像在ps2.0上用不了,不通用就不去研究了

5i365 发表于 2022-4-23 22:23

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254777&ptid=62461]13#[/url] [i]idwma[/i] [/b]


好的, 到时候我看到国外的类似的例子, 把代码贴过来, 在讨论一下

5i365 发表于 2022-4-26 22:42

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=254777&ptid=62461]13#[/url] [i]idwma[/i] [/b]


大侠好, win7和win10帮助系统还真是不统一, 上面的匹配正则不能通用, 现在突然有个新的想法, 就是获取在线帮助的示例, 看了一下, 非常有规律 一个示例标题, 一段代码, 如下图所示,
我已经用下面的代码, 获取到网页的内容了, 但是不知道怎么取示例的值, 感觉这个非常有挑战性, 貌似爬虫就是这一类的爬法

查了些资料, 好像说是用xpath定位到html元素, 然后就取到值了, 感觉应该能实现

$r = Invoke-RestMethod  (gcm sc).HelpUri
$r



[img]https://i.ibb.co/K0Pv1s1/abc.png[/img]

idwma 发表于 2022-4-26 23:01

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=255039&ptid=62461]15#[/url] [i]5i365[/i] [/b]


    不会

went 发表于 2022-4-26 23:49

[code]cls
$r = Invoke-WebRequest -Uri ((gcm sc).HelpUri)
$r.ParsedHtml.getElementsByTagName('h3') | Where-Object { $_.id -match '^example' } | foreach {
    '------{0}------' -f $_.innerText
    $_.nextSibling.nextSibling.innerText
}[/code]

5i365 发表于 2022-4-27 06:53

[i=s] 本帖最后由 5i365 于 2022-4-27 07:02 编辑 [/i]

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=255042&ptid=62461]17#[/url] [i]went[/i] [/b]


   感谢大侠帮忙, 测试了一下, 最后取的示例错了
链接
[url=http://go.microsoft.com/fwlink/?LinkID=113392]http://go.microsoft.com/fwlink/?LinkID=113392[/url]

取到的结果:
------Example 4: Use Filters with Set-Content------
The following command set the content all *.txt files in the C:\Temp directory to the Value empty.


应该取:
[font=SFMono-Regular, Consolas, &quot;]Set-Content[/font][font=SFMono-Regular, Consolas, &quot;] -Path[/font][color=#171717][font=SFMono-Regular, Consolas, &quot;] C:\Temp\*[/font][/color][font=SFMono-Regular, Consolas, &quot;] -Filter[/font][color=#171717][font=SFMono-Regular, Consolas, &quot;] *.txt[/font][/color][font=SFMono-Regular, Consolas, &quot;] -Value [/font][font=SFMono-Regular, Consolas, &quot;]"Empty"[/font][font=SFMono-Regular, Consolas, &quot;]
[/font]
[font=SFMono-Regular, Consolas]规律是:如果一行开头包含 powershell  结尾包含复制,它的下一行就是要取的内容[/font]
[font=SFMono-Regular, Consolas][img]https://i.ibb.co/FbCv6fZ/abc.png[/img]
[/font]

went 发表于 2022-4-27 12:34

[code]cls
$r = Invoke-WebRequest -Uri ((gcm sc).HelpUri)
$r.ParsedHtml.getElementsByTagName('h3') | Where-Object { $_.id -match '^example' } | foreach {
    '------{0}------' -f $_.innerText
    $ele = $_
    while($ele.tagName -ne 'PRE'){ $ele = $ele.nextSibling }
    $ele.innerText
}[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.