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
" URL- $uri - Status- $status "into[PsCustomObject]@{URL = $uri; Status = $status}and then output at the end with$result | Format-Table -AutoSize. Original code here$resultwill be an array of objects with two properties:URLandStatus. If you then print with$result | Format-Table -AutoSizeyou 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