Harvesting specific contents from Web Page

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of random commandline random commandline 3 months, 2 weeks ago.

  • Author
    Posts
  • #63940
    Profile photo of Sachin Ghagare
    Sachin Ghagare
    Participant

    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 

  • #63946
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    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]
            }
        }
    }
    
  • #63949
    Profile photo of random commandline
    random commandline
    Participant

    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']
        } 
    

You must be logged in to reply to this topic.