0

I need help to extract some values from a command:

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)

I tried to extract first value as below:

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

I need two more field: the ID and the URL (only if there is do.com in bindings). There might be many URLs in the binding. I need only the first one which has do.com all the remaining should be marked as null or blank.

A,1,csdev.do.com  
B,2,null  
C,3,ddprem.do.com  
D,4,null  
5
  • 4
    You may want to try the WebAdministration module instead of parsing appcmd output. Commented Oct 21, 2019 at 13:03
  • I get error. C:\Users\cs> Get-WebURL -PSPath "IIS:\Sites\A" Status ConnectFailure Commented Oct 21, 2019 at 14:06
  • Did you load the module (Import-Module WebAdministration)? Is IIS actually running? Commented Oct 21, 2019 at 14:11
  • yes it is running Commented Oct 21, 2019 at 14:13
  • I need to update in a csv file for 1000 servers, hence I need in the above format Commented Oct 21, 2019 at 14:21

1 Answer 1

1

While using the WebAdministration module seems like the best approach, you could try regex for this looping over the lines the command c:\windows\system32\inetsrv\appcmd list sites returns and parse the values you need from them.

Since I cannot test this for real myself, I'm using your example output from c:\windows\system32\inetsrv\appcmd list sites as a string array:

$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()
    }
}

Result:

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

Regex details:

^              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)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to get it in one command. c:\windows\system32\inetsrv\appcmd list sites | %{ $_.Regex(^SITE "(?<site>\w+)".+id:(?<id>\d+),bindings:(?:.+:(?<url>\w+\.do\.com))?)[1]; }. I guess there is some syntax issue here as I get error while executing
@Ash I wouldn't try to force it all to one single line if that is what you mean. Single-line code can become unreadable very quick and are notoriously error prone. Where in my code it says $siteList | ForEach-Object{ you could do c:\windows\system32\inetsrv\appcmd list sites | ForEach-Object{. Another way would be to make a function out of this and call that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.