繁体   English   中英

从 Windows PowerShell 中的命令中提取特定值

[英]Extract specific value from command in Windows PowerShell

我需要帮助从命令中提取一些值:

PS C:\Users\cs> c:\windows\system32\inetsrv\appcmd list sites
SITE "A" (id:1,bindings:http//:csdev.do.com,state:Stopped)
SITE "B" (id:2,bindings:tsd-gr2,state:Stopped)
SITE "C" (id:3,bindings:http/1028:8091:,http/19.28:80:ddprem.do.com,state:Stopped)
SITE "D" (id:4,bindings:http/109.149.232:80,state:Stopped)

我试图提取第一个值如下:

PS C:\Users\cs> c:\windows\system32\inetsrv\appcmd list sites | %{ $_.Split('\"')[1]; }
A
B
C
D

我还需要两个字段:ID 和 URL(仅当绑定中有 do.com 时)。 绑定中可能有许多 URL。 我只需要第一个有 do.com 所有剩余的应该标记为 null 或空白。

A,1,csdev.do.com  
B,2,null  
C,3,ddprem.do.com  
D,4,null  

虽然使用 WebAdministration 模块似乎是最好的方法,但您可以尝试使用正则表达式循环遍历命令c:\windows\system32\inetsrv\appcmd list sites返回的行并解析您需要的值。

由于我自己无法对其进行真实测试,因此我使用您的示例 output 来自c:\windows\system32\inetsrv\appcmd list sites列为字符串数组:

$siteList = 'SITE "A" (id:1,bindings:http//:csdev.do.com,state:Stopped)',
            'SITE "B" (id:2,bindings:tsd-gr2,state:Stopped)',
            'SITE "C" (id:3,bindings:http/1028:8091:,http/19.28:80:ddprem.do.com,state:Stopped)',
            'SITE "D" (id:4,bindings:http/109.149.232:80,state:Stopped)'

$regex = [regex] '^SITE "(?<site>\w+)".+id:(?<id>\d+),bindings:(?:.+:(?<url>\w+\.do\.com))?'
$siteList | ForEach-Object {
    $match = $regex.Match($_)
    while ($match.Success) {
        $url = if ($match.Groups['url'].Value) { $match.Groups['url'].Value } else { 'null' }
        '{0},{1},{2}' -f $match.Groups['site'].Value, $match.Groups['id'].Value, $url
        $match = $match.NextMatch()
    }
}

结果:

 A,1,csdev.do.com B,2,null C,3,ddprem.do.com D,4,null

正则表达式详细信息:

^              Assert position at the beginning of the string
SITE\ "        Match the characters “SITE "” literally
(?<site>       Match the regular expression below and capture its match into backreference with name “site”
   \w          Match a single character that is a “word character” (letters, digits, etc.)
      +        Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"              Match the character “"” literally
.              Match any single character that is not a line break character
   +           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
id:            Match the characters “id:” literally
(?<id>         Match the regular expression below and capture its match into backreference with name “id”
   \d          Match a single digit 0..9
      +        Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
,bindings:     Match the characters “,bindings:” literally
(?:            Match the regular expression below
   .           Match any single character that is not a line break character
      +        Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   :           Match the character “:” literally
   (?<url>     Match the regular expression below and capture its match into backreference with name “url”
      \w       Match a single character that is a “word character” (letters, digits, etc.)
         +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      \.       Match the character “.” literally
      do       Match the characters “do” literally
      \.       Match the character “.” literally
      com      Match the characters “com” literally
   )
)?             Between zero and one times, as many times as possible, giving back as needed (greedy)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM