[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

830 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