Хотя использование модуля WebAdministration кажется лучшим подходом, вы можете попробовать регулярное выражение для этого цикла по строкам, которые возвращает команда c:\windows\system32\inetsrv\appcmd list sites
, и проанализировать нужные вам значения из них.
Поскольку я не могу это проверитьна самом деле я использую ваш пример вывода из 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)