[英]Running commands over Plink from PowerShell fails with “■e: not found”
Thanks for your help. 谢谢你的帮助。 So I have a problem with my script.
所以我的脚本有问题。 I am trying to convert my script from batch to PowerShell, mostly just to try to learn it so I am completely new to PowerShell but I know other languages so I am decent at programming.
我正在尝试将脚本从批处理转换为PowerShell,主要是为了尝试学习它,因此我对PowerShell完全陌生,但是我知道其他语言,因此我擅长编程。 My script creates a
kivaCommands.txt
file that is then used in Plink to be executed on the server. 我的脚本创建了一个
kivaCommands.txt
文件,然后在Plink中使用该文件在服务器上执行。 My batch script works fine with Plink but when I converted to PowerShell the Plink line errors on the -m
switch. 我的批处理脚本在Plink上工作正常,但是当我转换为PowerShell时,
-m
开关上的Plink行错误。 I get 我懂了
sh: ■e: not found
I get this error no matter what commands are in the txt file, even if the file is empty. 无论txt文件中有什么命令,即使该文件为空,我都会收到此错误。 The Plink line works fine if I pass it a single command to execute.
如果我将单个命令传递给Plink行,则该行工作正常。 This is the code I'm having trouble with.
这是我遇到麻烦的代码。
$hostLogin = "myServer"
$passwd = "myPassword"
$command = "H:\kivaCommands.txt"
C:
cd \
cd "Program Files (x86)\PuTTY"
./PLINK.EXE -ssh $hostLogin -P 22 -pw $passwd -m $command
The cd
commands were the only way I could get it to work. cd
命令是我可以使用它的唯一方法。 I tried all other ways of giving the whole path and creating a variable for the path but Plink would not execute. 我尝试了其他所有方法来给出整个路径并为该路径创建变量,但Plink无法执行。
So after more troubleshooting, I have narrowed it down to the file created. 因此,在进行更多故障排除之后,我将其范围缩小到了创建的文件。 If I manually create the file it works fine but my script creates the file before calling Plink.
如果我手动创建文件,则可以正常工作,但是我的脚本在调用Plink之前创建了文件。 I have tried three different ways to create the file
我尝试了三种创建文件的方法
"exit ^| sqlplus / @kiva_extract $assessorYear" | Set-Content H:\kivaCommands.txt -Encoding Unicode
"exit ^| sqlplus / @kiva_extract $assessorYear" | Out-File H:\kivaCommands.txt -Encoding Unicode
"exit ^| sqlplus / @kiva_extract $assessorYear" > H:\kivaCommands.txt
The file creates fine and looks right but Plink cannot open correctly. 该文件可以正常运行,但外观正确,但是Plink无法正确打开。
The UTF-8 output file generated by PowerShell starts with UTF-8 BOM mark . PowerShell生成的UTF-8输出文件以UTF-8 BOM标记开头。
Unix system typically do not expect BOM, so the remote shell tries to interpret it as a command, failing. Unix系统通常不希望使用BOM,因此远程外壳程序会尝试将其解释为命令,但会失败。 The BOM is that square character (
■
) in the error message: BOM表是错误消息中的方形字符(
■
):
sh: ■e: not found
I do not know how to prevent the BOM from appearing in the output. 我不知道如何防止BOM表出现在输出中。
But you can strip it ex-post: 但是您可以事后删除它:
$MyPath = "H:\kivaCommands.txt"
$MyFile = Get-Content $MyPath
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($MyPath, $MyFile, $Utf8NoBomEncoding)
See Using PowerShell to write a file in UTF-8 without the BOM 请参阅使用PowerShell在没有BOM的情况下以UTF-8格式写入文件
Try executing it like this: 尝试像这样执行它:
& 'C:\Program Files (x86)\PuTTY\Plink.exe' -ssh $hostLogin -P 22 -pw $passwd -m $command
When an exe path has spaces in it, you have to quote the path. 如果exe路径中有空格,则必须引用该路径。 However a quoted path to PowerShell is
just
a string. 但是,PowerShell的引用路径
just
一个字符串。 You need to tell PowerShell to execute the command named (or exe pointed to) by the string. 您需要告诉PowerShell执行该字符串命名的命令(或指向的exe)。 That is what the call operator
&
does. 这就是呼叫操作员
&
所做的。 It says "execute the string to the right of me". 它说“执行我右边的字符串”。 Note that the string is just the command name and should not contain any arguments to the command.
请注意,该字符串只是命令名称,不应包含该命令的任何参数。
I have run into this issue with tst10 and found that instead of using the encoding Unicode try ASCII . 我在tst10中遇到了这个问题,发现不是使用Unicode而是尝试使用ASCII编码。 The reason being it's the plink was looking for a windows txt file.
原因是它是plink在寻找Windows txt文件。
Text files are in ASCII format, so the following line would not be: 文本文件为ASCII格式,因此以下行不会是:
^| sqlplus / @kiva_extract $assessorYear" | Out-File H:\kivaCommands.txt -Encoding Unicode
it would be: 这将是:
^| sqlplus / @kiva_extract $assessorYear" | Out-File H:\kivaCommands.txt -Encoding ASCII
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.