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

从Windows PowerShell中的命令中提取特定值,windows,powershell,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.co

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

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列表站点 站点“A”(id:1,绑定:http/:csdev.do.com,状态:Stopped) 站点“B”(id:2,绑定:tsd-gr2,状态:已停止) 站点“C”(id:3,绑定:http/1028:8091:,http/19.28:80:ddprem.do.com,状态:Stopped) 站点“D”(id:4,绑定:http/109.149.232:80,状态:已停止) 我尝试提取第一个值,如下所示:

PS C:\Users\cs> c:\windows\system32\inetsrv\appcmd list sites | %{ $_.Split('\"')[1]; } A B C D PS C:\Users\cs>C:\windows\system32\inetsrv\appcmd列出站点|%{$\.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 A、 1,csdev.do.com B、 2,空 C、 3,ddprem.do.com D、 4,空
虽然使用WebAdministration模块似乎是最好的方法,但您可以尝试使用regex在命令
c:\windows\system32\inetsrv\appcmd list sites
返回并解析所需的值的行上循环

由于我自己无法对其进行实际测试,因此我将使用
c:\windows\system32\inetsrv\appcmd列表站点的示例输出作为字符串数组:

$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()
    }
}
正则表达式详细信息:

^              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)
^在字符串开头断言位置
SITE\“按字面意思匹配字符“SITE”
(?匹配下面的正则表达式,并将其匹配捕获到名为“site”的backreference中
\w匹配一个“单词字符”(字母、数字等)的单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)
按字面意思匹配字符“”
.匹配不是换行符的任何单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
id:按字面意思匹配字符“id:”
(?匹配下面的正则表达式,并将其匹配捕获到名为“id”的反向引用中)
\d匹配单个数字0..9
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)
,绑定:匹配字符“,绑定:”字面意思
(?:匹配下面的正则表达式
.匹配不是换行符的任何单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
:按字面意思匹配字符“:”
(?匹配下面的正则表达式,并将其匹配捕获到名为“url”的反向引用中)
\w匹配一个“单词字符”(字母、数字等)的单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
\.按字面意思匹配字符“.”
按字面意思匹配“do”字符
\.按字面意思匹配字符“.”
com按字面意思匹配字符“com”
)
)?在0到1次之间,尽可能多次,根据需要回馈(贪婪)

您可能希望尝试而不是解析
appcmd
输出。我得到错误。C:\Users\cs>获取WebURL-PSPath“IIS:\Sites\A”状态连接失败是否加载模块(
导入模块WebAdministration
)?IIS是否正在运行?是的,它正在运行我需要在1000台服务器的csv文件中进行更新,因此我需要上面的格式是否有任何方法可以通过一个命令获得它。c:\windows\system32\inetsrv\appcmd list sites |%{$\u.Regex(^SITE”(?\w+)+id:(?\d+),绑定:(?:+:(?:(?\w+\.do\.com))[1]}.我想这里有一些语法问题,因为我在executing@Ash如果你的意思是这样的话,我不会试图将所有内容强制放在一行中。单行代码很快就会变得不可读,并且是出了名的容易出错。在我的代码中,
$siteList | ForEach对象{
您可以执行
c:\windows\system32\inetsrv\appcmd list sites | ForEach对象{
。另一种方法是利用此对象生成函数并调用它。
^              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)