Solved

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

Posted on 2011-09-28
4
1,397 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Charlie_Melega
  • 3
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 36719663
specify encoding:

-Encoding Ascii
0
 
LVL 35

Expert Comment

by:YZlat
ID: 36719680
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 36719723
try something like

function Test-Site {
    param($URL)
        trap{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" | out-file -path "ErrorLog.txt" -encoding ascii
        exit 1
        }
0
 

Author Closing Comment

by:Charlie_Melega
ID: 36720505
YZlat, thanks, this is exactly what I was looking for.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now