【问题标题】:how vbscript read the result file generated by powershell script (called by the vbscript itself) ?vbscript 如何读取由 powershell 脚本生成的结果文件(由 vbscript 本身调用)?
【发布时间】:2021-09-20 19:51:10
【问题描述】:

我有一个 vbscript 来执行一个 powershell 脚本。 powershell 脚本只是将结果附加到文件中。 然后在powershell脚本执行完成后返回vbscript。 vbscript 将读取文件的内容并将其显示在控制台上。 不幸的是,powershell 无法生成 vbscript 读取的结果文件。

如果我将行更改为 ErrCode = objShell.run("powershell.exe -file writeFile.Ps1", 0), powershell 脚本可以生成结果文件。 但是 vbscript 永远无法正确显示结果文件的内容,或者它已损坏。 如果我从记事本打开结果文件,我可以正确查看内容。 你知道背后的问题是什么吗?

这里是源代码:

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

writeFile.Ps1的源代码:

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

谢谢。

【问题讨论】:

    标签: powershell vbscript


    【解决方案1】:

    在 Out-File 上,尝试使用参数“-Encoding ASCII”。默认情况下,Out-File 以 Unicode 格式输出。

    【讨论】:

      【解决方案2】:

      要么让 PowerShell 使用 ASCII 编码创建文件为 Keith Hill 建议:

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

      或在 VBScript 中以 Unicode 格式打开:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-22
        • 2010-10-25
        • 1970-01-01
        • 1970-01-01
        • 2020-12-10
        • 2010-11-05
        • 2022-01-26
        • 1970-01-01
        相关资源
        最近更新 更多