Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-28
4
Medium Priority
?
1,474 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 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 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