0

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. My script creates a kivaCommands.txt file that is then used in Plink to be executed on the server. My batch script works fine with Plink but when I converted to PowerShell the Plink line errors on the -m switch. 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. The Plink line works fine if I pass it a single command to execute. 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. I tried all other ways of giving the whole path and creating a variable for the path but Plink would not execute.

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. 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.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
bbishopca
  • 286
  • 3
  • 12
  • What happens if your run the same command on Windows console (`cmd.exe`)? I.e. `PLINK.EXE -ssh myServer -P 22 -pw myPassword -m H:\kivaCommands.txt` – Martin Prikryl Jul 08 '15 at 20:48
  • I works fine in the command prompt. Its only having an issue with powershell. – bbishopca Jul 08 '15 at 21:10
  • Even with exactly the same `kivaCommands.txt` file? – Martin Prikryl Jul 08 '15 at 21:27
  • Yes. I copied the line straight out of the powershell script and pasted it into cmd and it worked fine. However, powershell will not take the -m switch. Its driving me crazy. I have tried many different drives and locations of the file as well. It finds the file because I get a different error if the file doesn't exist. – bbishopca Jul 08 '15 at 21:39
  • After your edit, it seems you actually were not using the same `kivaCommands.txt`. – Martin Prikryl Jul 09 '15 at 05:40

3 Answers3

1

The UTF-8 output file generated by PowerShell starts with UTF-8 BOM mark.

Unix system typically do not expect BOM, so the remote shell tries to interpret it as a command, failing. The BOM is that square character () in the error message:

sh:  ■e:  not found

I do not know how to prevent the BOM from appearing in the output.

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

Community
  • 1
  • 1
Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
  • This is not the solution I did but what you said about the BOM was correct. I thought I had tried it with a manually created file before but I guess I hadn't. When I created the file myself it worked. This is what I did for the file creation and it worked. I'm not exactly sure why. I added the line `If (Test-Path $commandFile) { Clear-Content H:\kivaCommands.txt }Else{ New-Item H:\kivaCommands.txt -type file }` to create my file and that took care of the issue. Thanks – bbishopca Jul 09 '15 at 14:48
0

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. However a quoted path to PowerShell is just a string. You need to tell PowerShell to execute the command named (or exe pointed to) by the string. 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.

Keith Hill
  • 173,872
  • 36
  • 316
  • 347
  • Thanks that helped me get rid of all the change directories but I still get the error trying to use the -m switch to pass a file of commands. – bbishopca Jul 08 '15 at 21:12
  • Does the -m parameter expect the argument to be quoted? If so, try this: `-m ""$command""` Replace with the backtick char. – Keith Hill Jul 09 '15 at 02:08
  • The error `sh: ■e: not found` indicates the `-m` actually "works" (some commands are executed). The problem is with the file contents. – Martin Prikryl Jul 09 '15 at 05:48
0

I have run into this issue with tst10 and found that instead of using the encoding Unicode try ASCII. The reason being it's the plink was looking for a windows txt file.

Text files are in ASCII format, so the following line would not be:

^| 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
stig-js
  • 1,951
  • 12
  • 24