Powershell在回显到文件时生成未知字符 [英] Powershell generating unknown character when echoing to file

查看:80
本文介绍了Powershell在回显到文件时生成未知字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Powershell 将 3 行纯文本行回显到文件中:

I am trying to echo 3 plain-text lines to a file using Powershell:

echo "#Generated" > psftp.scp
echo "put test.txt" >> psftp.scp
echo "quit" >> psftp.scp

然后我使用 psftp.exe 批处理模式来运行该文件(在 SFTP 中执行命令),但是 psftp 看到无效字符时出错:

I then use psftp.exe batch mode to run the file (executes the commands in SFTP), but psftp errors out seeing an invalid character:

psftp: unknown command " ■#"

我错过了什么?我可以在 Windows 记事本中手动输入文件,它 (psftp) 可以工作.无论我如何将第一行更改为 (#Generated),第一部分中的块符号都会出现此错误.

What am I missing? I can manually type up the file in Windows Notepad and it (psftp) works. No matter what I change the first line to (#Generated) it gets this error with the block symbol in the first part.

我试过在 NotePad++ 中查看文件,打开显示所有符号",但只看到 CR &LF 在行尾,这是正常的.

I've tried viewing the file in NotePad++ w/ "Show All Symbols" on, but only saw CR & LF at the end of lines which is normal.

推荐答案

尝试使用 set/add-content 而不是重定向.您可能还需要设置编码.

Try using set/add-content instead of redirection. You might also need to set the encoding.

"#Generated" | set-content psftp.scp -Encoding Ascii
"put test.txt" | add-content psftp.scp -Encoding Ascii
"quit" | add-content psftp.scp -Encoding Ascii

这篇关于Powershell在回显到文件时生成未知字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆