Link to home
Start Free TrialLog in
Avatar of Charlie_Melega
Charlie_Melega

asked on

Powershell script outputs to UTF encoded text file, need ASCII encoded instead

Hello,

I have a PowerShell script that was created for me by a helpful expert on this board. The script will perform check on certain websites (connectivity check) and output any failures to a errorlog.txt file. Everything is fantastic with the exception that the information outputted into errorlog.txt file is actually in UTF encoding.  Another components attempt to read this errorlog.txt but fails because it is UTF encoded. I need to have this errolog.txt file in ASCII encoding. Is there any easy way to specify ASCII encoding for the errorlog.txt file within the PoweerShell script or is the only choice to convert  this file to ASCII encoding after the fact?
Here is the script:



Thanks in advance for help.

function Test-Site {
    param($URL)
        trap{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"
        exit 1
        }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
} 

For ($i;$i -lt 10;$i++){
Test-Site "http://www.test.org"
}

########################
#End Script

Open in new window

Avatar of YZlat
YZlat
Flag of United States of America image

specify encoding:

-Encoding Ascii
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charlie_Melega
Charlie_Melega

ASKER

YZlat, thanks, this is exactly what I was looking for.