jakeythehobo jakeythehobo - 3 months ago 15
PowerShell Question

Renaming Variable Output in PowerShell

I'm trying to write a script to monitor URLs. I've managed to get the information I want, and my script looks like this

$logfile = "C:\LogFileTest.log"
Function WriteHeader-Websites {
[cmdletbinding()]
param (
[string]$URL
)
if ($logfile -eq "nologfile"){
write-host
write-host "$URL"
write-host
}
else {
add-content -path $logfile -value ""
add-content -path $logfile -value "$URL"
add-content -path $logfile -value ""
}
}

Function Test-Websites {
$URLs = Get-Content -Path C:\WebsiteChecks.txt
foreach ($URL in $URLs) {
$ArrayLine =$Line.split(",")
$
$Line.name =
try {
$request = [System.Net.WebRequest]::Create($URL)
$response = $request.GetResponse()
WriteHeader-Websites "$URL is available!"
} catch {
WriteHeader-Websites ("$URL failed! The error message was '{0}'." -f $_)
} finally {
if ($response) {
$response.Close()
Remove-Variable response
}
}
}
}

Test-Websites


My text file looks like this:

http://www.google.com
http://www.bing.com
http://www.bbc.co.uk


And the output from the script looks like this:

http://www.google.com is available!


I'd like to be able to add a short name for the websites in the text file, so it uses that name instead.

An example of the text file would be:

http://www.google.com,Google Website
http://www.bing.com,Bing Website


and I'd like the return to look like:

Google Website is available!
Bing Website is available!


or

Google Website failed! Error message is...


But I have no idea how to do that, or what I'd even Google for to find it out. Any suggestions?

Thanks

Answer

The easiest way to do this would be to create a csv with headers, then you can refer to the columns using their headers.

url,description
http://www.google.com,Google Website
http://www.bing.com,Bing Website

Then use Import-Csv to import the file, so you could do something like this:

Import-Csv -Path C:\WebsiteChecks.txt | % {
  $_.description
  $_.url
}

Update Here's your code rewritten using Import-Csv notice I put description in it's own variable as $_ would be updated by the exception in your catch statement.

$logfile = "C:\LogFileTest.log"

Function WriteHeader-Websites {
    [cmdletbinding()]
    param (
        [string]$URL
    )
    if ($logfile -eq "nologfile"){
        write-host 
        write-host "$URL"
        write-host
    }
    else {
        add-content -path $logfile -value ""
        add-content -path $logfile -value "$URL"
        add-content -path $logfile -value ""
    }
}

Function Test-Websites {

    Import-Csv -Path C:\WebsiteChecks.txt | % {

    $url = $_.url
    $description = $_.description

    try {
        $request = [System.Net.WebRequest]::Create($url)
        $response = $request.GetResponse()
        WriteHeader-Websites "$description is available!"   
    } catch {
        WriteHeader-Websites ("$description failed! The error message was '{0}'." -f $_)
    } finally {
            if ($response) {
                $response.Close()
                Remove-Variable response
            }
        }
    }
}

Test-Websites