【问题标题】:batch file doesnt read powershell edited text file correctly批处理文件无法正确读取 powershell 编辑文本文件
【发布时间】:2018-04-06 20:57:29
【问题描述】:

我有一个写入文本文件的 powershell 脚本

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

以及将文本文件的内容读入变量的批处理文件。

set /p FAILOVR_STS = <FailoverStatus.txt

问题是批处理文件没有正确读取文本文件。从批处理输出中可以看出,在被 shell 脚本编辑后,似乎发生了一些转换。它读取 B 而不是 Backup

不知道发生了什么。如果我创建一个新的文本文件并对其进行编辑,批处理文件会正确读取它。

任何建议将不胜感激。

【问题讨论】:

  • 创建文本文件时,powershell 不默认为 Unicode。我相信您需要在创建 FailOverStatus.txt 文件时告诉它使用 ascii。
  • 好点..会尝试让你知道
  • @Squashman 成功了。非常感谢。在 powershell 脚本中将编码设置为 ascii $text = "Primary" |Out-File \\EMS-DS-01A\WINRedundancy\FailoverStatus.txt -Encoding ascii
  • 旁注:您的 powershell 的 $text = 部分毫无意义。您显式输出到文件,因此$text 变量不会捕获任何内容。

标签: powershell batch-file


【解决方案1】:

当您运行Out-File 时,您需要对其进行编码ascii

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

删除$text,因为它是不必要的

来自Get-Help Out-File -Online(Microsoft Cmdlet 文档)

-Encoding
Specifies the type of character encoding used in the file.
Unicode is the default.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2012-02-27
    • 2015-01-22
    • 2019-12-02
    • 2013-09-14
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多