1

I have a list of application web URLs, wherein I am trying to monitor HTTP statuscode of the web URL by Invoke-WebRequest command method.

I got the list objects by below command:

Invoke-WebRequest https://example.com/123 | Get-Member

Here, I am using statuscode and statusdescription fields to include it in output:

Invoke-WebRequest https://example.com/123 |
    Select-Object StatusCode, StatusDescription |
    ft -a

Now I wish to include a custom name of URL in output before statuscode and statusdescription so that output will look like below

URL          StatusCode StatusDescription
---          ---------- -----------------
abc web page        200 OK       
6
  • 3
    Sounds cool. What have you tried to make it work? Commented Feb 12, 2019 at 11:06
  • Hi, welcome to stackoverflow, kindly please read minimal reproducible example and review the post, otherwise you can expect closing because its so general and doesnt mentioning any self effort, we are here to help you, but not to do the tasks instead of you :). Thanks in advance. Commented Feb 12, 2019 at 11:08
  • Personally, I can suggest you to define an array with the all addresses, then iterate over this given array, and print out the values (for URL you can use index in array) Commented Feb 12, 2019 at 11:09
  • I might be horribly wrong in interpreting how to write code, but I wrote below lines before original code , $URL = Add-Member -NotePropertyName URL -NotePropertyValue abc web page and Invoke-WebRequest example.com/123 | Select-Object URL, StatusCode, StatusDescription | ft -a Do I need to write Add-object instead of add-member? Commented Feb 12, 2019 at 11:10
  • Doesnt matter how correct or incorrect your code will be, just try to take a look for some info over the internet and try to do your best, if you will stuck on something specific, I am pretty sure you will get the help over there. People just dont wanna be bothered by general and broad questions, because it's hard to answer to them, eg. in this case, there are many ways.. Add-Member is "adding custom parameter to the variable" for further usage, eg., if you dont need/want to store values, it can be pretty more easier, eg. Commented Feb 12, 2019 at 11:14

2 Answers 2

2

If you dont need to store data in variables, and just print is fine for you

$URLsources = "http://fooo/fooURL1:800","http://fooo/fooURL2:800","http://fooo/fooURL2:800"

#table definition
    $tabName = "Output table"

    #Create Table object
    $table = New-Object system.Data.DataTable “$tabName”

    #columns definition
    $col1URL = New-Object system.Data.DataColumn URL,([string])
    $col2Status = New-Object system.Data.DataColumn Status,([string])
    $col3Desc = New-Object system.Data.DataColumn Desc,([string])

    #add columns
    $table.Columns.Add($col1URL)
    $table.Columns.Add($col2Status)
    $table.Columns.Add($col3Desc)


foreach($url in $URLsources){
  $result = Invoke-WebRequest $url 

  #preparation of the row
      $row = $table.NewRow()
      $row.URL= $url
      $row.Status= $result.StatusCode
      $row.Desc= $result.StatusDescription

  #add row to the table  
  $table.Rows.Add($row)

}

#print out the table
$table | format-table -AutoSize 
Sign up to request clarification or add additional context in comments.

2 Comments

Welcome, just for the next time please try to investigate on your own first, actually with my minimal knowledge of powershell took me ~15 min of work with testing :) Will appreciate if you will mark answer as accepted.
check also @guiwhatsthat solution, but did not worked for me, maybe another PS version or something
2

You can add everything to your output:

Invoke-WebRequest www.google.com | Select-Object -Property @{n="URL";e={'Any Name'}},StatusCode, StatusDescription

If you want the real url which you checked in your output you could do it like that:

Invoke-WebRequest www.google.com | Select-Object -Property @{n="URL";e={$_.BaseResponse.ResponseUri.Host}},StatusCode, StatusDescription

1 Comment

Thanks a lot... This is a simple way to solve this... Many thanks

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.