Author Posts

February 10, 2017 at 3:34 pm

Hi,

I'm newbie to Powershell.

As part of routine health check, i need to monitor several web application servers manually using URL for each server. just wondering if I could get status of all web server components in single file (html/txt/csv) would make my job easy.

to start, I used 'Invoke-webrequest' followed by RawContent where I see expected output (ex. Cognos-ContentManager-State: running in below expample) but with more un-necessary information.
so how do I get specific contents from each URL...

I'm expecting output something like,
URL Component Status
url1 Running
url2 Running
url3 Standby

 PS C:\WINDOWS\system32> $webpage=Invoke-webrequest "http://myserver:9300/p2pd/servlet"
$webpage.RawContent 

Output
———-
HTTP/1.1 200 OK 
X-Cognos-ContentManager-State: running
Content-Length: 712 
Content-Type: text/html 
Date: Fri, 10 Feb 2017 14:28:10 GMT 
Server: Apache-Coyote/1.1 

February 10, 2017 at 4:19 pm

Please check if below works for you:

$uriList = @(
    'http://site1'
    'http://site2'
)

foreach ($uri in $uriList) {
    $response = Invoke-WebRequest -Uri $uri -UseBasicParsing
    if ($response.RawContent -match 'X-Cognos-ContentManager-State: (.+)') {
        [PSCustomObject] @{
            URL= $uri
            ComponentStatus = $Matches[1]
        }
    }
}

February 10, 2017 at 4:41 pm

That looks like header info.

$uri = @('one.com','two.com')

foreach ($u in $uri){
    $test = Invoke-WebRequest -Uri $u
   [PSCustomObject]@{
        URL = $u
        Status = $test.Headers['X-Cognos-ContentManager-State']
    }