0
votes

I have one vbscript to execute a powershell script. The powershell script simply append the result to a file. Then after the powershell script execution complete and return to vbscript. The vbscript will read content of the file and display it on console. Unfortunately the powershell cannot generate the result file to be read by the vbscript.

If I change the line to ErrCode = objShell.run("powershell.exe -file writeFile.Ps1", 0), The powershell script can generate the result file. But the vbscript can never display the content of the result file correctly or it is corrupted. If I open the result file from Notepad, I can view the content correctly. Do you have any idea what is the problem behind?

Here are the source code:

Source code for readFile.vbs:\

resultFile = "writeFile.Ps1.txt"
set objShell = CreateObject("WScript.Shell")
ErrCode = objShell.run("powershell.exe -nologo -command writeFile.Ps1", 0)
Set objReadFile = CreateObject("Scripting.FileSystemObject")
Set strLines = objReadFile.OpenTextFile(resultFile, 1)
Do While Not strLines.AtEndOfStream 
    strLine =  strLines.ReadLine()
    WScript.Echo strLine
Loop
Set objReadFile = Nothing

Source code for writeFile.Ps1:

$ResultFilePath = "writeFile.Ps1.txt"
If (Test-Path $ResultFilePath) {
    Remove-Item $ResultFilePath
}
Get-Date | out-file $ResultFilePath
Exit 0

Thanks.

2

2 Answers

2
votes

On the Out-File, try using the parameter "-Encoding ASCII". By default Out-File outputs in Unicode.

1
votes

Either make PowerShell create the file with ASCII encoding as Keith Hill suggested:

Get-Date | Out-File $ResultFilePath -Encoding ASCII

or open it as Unicode in VBScript:

Set strLines = objReadFile.OpenTextFile(resultFile, 1, False, -1)