0

Need some help in getting output for the below code as table format, below is the code used.

$URLList = Get-Content  C:\temp\link.txt



$result = foreach ($uri in $URLList)  { 
    try{
        $res =  Invoke-WebRequest -Uri $uri -UseDefaultCredentials -UseBasicParsing -Method Head -TimeoutSec 5 -ErrorAction Stop
        $status = [int]$res.StatusCode  
      
       
        
        
    } 
    catch {
        $status = [int]$_.Exception.Response.StatusCode.value__
    }
    # output a formatted string to capture in variable $result
    " URL- $uri - Status- $status " 
}  

#print
$result

Currently result received as below

URL- http://example.com - Status- 200 

Need it as below in table format,

| URL                  | Status |

| http://example.com   |200     |

" When the output is send via email i am getting as below, enter image description here

Expected email output like this expected output via email

6
  • Format-Table? Commented Apr 11, 2024 at 9:17
  • Just change " URL- $uri - Status- $status " into [PsCustomObject]@{URL = $uri; Status = $status} and then output at the end with $result | Format-Table -AutoSize. Original code here Commented Apr 11, 2024 at 9:39
  • Helo @Theo, - it worked with the correct headers but it doenst have a table format, with rows and columns, can you please help with that ? i trying to output this to an email report Commented Apr 11, 2024 at 9:51
  • What do you mean by but it doenst have a table format ? If you output an object inside the loop as I have commented, then $result will be an array of objects with two properties: URL and Status. If you then print with $result | Format-Table -AutoSize you will get a table in the console output. If you want to capture that as formatted table (text) for use in an email, do $tableText = $result | Format-Table -AutoSize | Out-String Commented Apr 11, 2024 at 9:57
  • HI Theo, i am trying to get output in rows and column in an email , currently even using $tabletext it is not in a correct format. Commented Apr 11, 2024 at 10:16

1 Answer 1

0

UPDATE 2: added code to send by email

UPDATE: added code to show a "pretty" table of the results.

This work for a one-time simple thing, but for better management of formatting outputs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7.4

Original:
Do not output a string, but a PSCustomObject.
You can then pipe it to Format-Table (which is often unnecessary as Powershell often uses it automatically for PSCustomObjects but it does not hurt to force it)

$result = foreach ($uri in $URLList) { 
    Write-Verbose 1
    try {
        $res = Invoke-WebRequest -Uri $uri -UseDefaultCredentials -UseBasicParsing -Method Head -TimeoutSec 5 -ErrorAction Stop
        $status = [int]$res.StatusCode  
    } 
    catch {
        $status = [int]$_.Exception.Response.StatusCode.value__
    }

    [PSCustomObject]@{
        URL    = $uri
        Status = $status
    }
}  

# convert to HTML, add CSS to add visible borders, then cast to a single string
[string]$Body = $result | ConvertTo-Html | ForEach-Object { $_ -replace '<(td|th)>', '<$1 style="border: 1px solid black">' }    

# added -BodyAsHtml to tell the system it's not pure text.
send-mailmessage -to "[email protected]" -from "[email protected]" -subject "URL/API's Status Alert" -Body $Body -BodyAsHtml -SmtpServer $SMPTServer -Port $port -UseSsl -Credential $credential 
Sign up to request clarification or add additional context in comments.

9 Comments

Yep, that's what I already commented, but apparently this is still not the correct format. I've asked the OP to elaborate
@Theo updated with what OP wants
Let's hope so.. ;)
@Theo and sirtao - thanks for helping out, the format when outputting in powershell console is fine,but when send via email the format is gone and no rows or columns available, i will try to attach picture in the thread for your convenience
How are you passing the results to the email?
|

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.