1

I have a powershell script that writes to a text file

$text = "Primary" |Out-File \\DS-01A\WINRedundancy\FailoverStatus.txt

Text File

and a batch file that reads in the contents of the text file into a variable.

set /p FAILOVR_STS = <FailoverStatus.txt

The problem is that the batch file does not read the text file correctly. It looks like some conversion is happening after being edited by the shell script as evident in the batch output. It reads B instead of Backup

Batch Output

Not sure what is going on. If i create a new text file and edit it, the batch file reads it correctly.

Any suggestions would be appreciated.

5
  • 3
    Doesn't powershell default to Unicode when creating a text file. I believe you need to tell it to use ascii when creating the FailOverStatus.txt file. Commented Oct 25, 2017 at 16:55
  • great point..will try that and let you know Commented Oct 25, 2017 at 16:58
  • Example 3: learn.microsoft.com/en-us/powershell/module/… Commented Oct 25, 2017 at 16:59
  • @ Squashman That worked. Thank you very much .In the powershell script set encoding to ascii $text = "Primary" |Out-File \\EMS-DS-01A\WINRedundancy\FailoverStatus.txt -Encoding ascii Commented Oct 25, 2017 at 17:01
  • 1
    Side Note: The $text = portion of your powershell is pointless. You explicitly output to a file, so there is nothing to be captured with the $text variable. Commented Oct 25, 2017 at 17:30

1 Answer 1

2

When you run Out-File you need to encode it ascii

'Primary' | Out-File \\DS-01A\WINRedundancy\FailoverStatus.txt -Encoding ascii

Removed $text because it is unecessary

From Get-Help Out-File -Online (Microsoft Cmdlet Documentation)

-Encoding
Specifies the type of character encoding used in the file.
Unicode is the default.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.