Bunion
Bunion

Reputation: 461

Converting Invoke-Webrequest to string to use in a csv file, trouble with format

I'm trying to get the output of the ppro status website into a csv format that i can then use i've got the data that i want with the below, but i'm having trouble formatting it into a csv can someone advise what I'm doing wrong?

$URL = "https://status.ppro.com/"
$req = Invoke-Webrequest -URI $URL
$name = $req.ParsedHtml.getElementsByClassName("name") | ForEach-Object{Write-Output $_.innerhtml}
$Status = $req.ParsedHtml.getElementsByClassName("component-status") | ForEach-Object{Write-Output 
$_.innerhtml} 

$Counter=0
foreach($row in $name)
{

$nameString = $name[$Counter] | Out-String
$statusString = $Status[$Counter] | Out-String
$Output += Write-Output "$nameString, $statusString"
#$hash[$nameString] = $statusString

$Counter++
}
Write-Output "Name,status"
$Output | Out-File  -FilePath "C:\Users\test\test123.csv" 

Example Output of what i'm trying to achieve is below

Name, Status

Girogate: Transaction Processing Platform, Operational

Girogate: Transaction Redirector, Operational

Thanks

Upvotes: 1

Views: 486

Answers (1)

Darrell
Darrell

Reputation: 154

Perhaps not ideal but this does mostly work:

$Output=$null
# Create the output file in the invocation path\Logs with a date-coded name
# The .\Logs file must exist prior to execution.
$OutFile = $MyInvocation.MyCommand.Path.Replace($MyInvocation.MyCommand.Name,"Logs\") + ((Get-Date).AddDays(-1).ToString('yyyy_MM_dd')) + ".csv"
$URL = "https://status.ppro.com/"
$req = Invoke-Webrequest -URI $URL
$name = $req.ParsedHtml.getElementsByClassName("name") | ForEach-Object{Write-Output $_.innerhtml}
$Status = $req.ParsedHtml.getElementsByClassName("component-status") | ForEach-Object{Write-Output $_.innerhtml} 

$Counter=0
Write-Output "Name,status"
foreach($row in $name)
{

$nameString = (($name[$Counter]).trim()).replace("`r","").replace("`n","")
$statusString = (($Status[$Counter]).replace("`n","").replace("`r","")).trim()  | Out-String
$Output += Write-Output "$nameString,$statusString"
#$hash[$nameString] = $statusString
$Counter++
}
$Output > $OutFile

Upvotes: 2

Related Questions