Solved

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

Posted on 2011-09-28
4
1,414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 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