3

I've got a bat script that does a few cmds and also runs a stack of powershell scripts. Sometimes it errors out on the second or third command because it's cutting off a few characters. It's also done this on xcopy commands following a powershell script and vice versa. So here is an example: Bat file:

xcopy "\\server\####\####\######" "C:\######" /S/I/E/Y
powershell "C:\SetupVM.ps1"

output:

\\server\Hard Disks\******.vhdx
\\server\Virtual Machines\*******.XML
2 File(s) copied

C:\Users\*****>rshell "C:\SetupVM.ps1"
'rshell' is not recognized as an internal or external command,
operable program or batch file.

Has anyone seen this before? Any ideas on how to avoid it?

Gavin Greenwalt
  • 325
  • 4
  • 10
  • 3
    My guess is an encoding issue with your bat file. Which editor are you using? Can you Ctrl+C/Ctrl+V the whole thing into notepad, save as a new .cmd and see if it helps? – Ryan Bemrose Jun 02 '15 at 17:24
  • I could try but I'm actually already doing it in Notepad to avoid any possible hidden unicode characters and such. Also it only happens about 1/4 of the time. So it's not predictable. I'll run the same script on 5 machines and only 1 of them will error out. I'm also going to try moving the /s /i /e /y to before the paths. Maybe it's not reading them for some reason as flags but escape characters... it's a weird one however you slice it. – Gavin Greenwalt Jun 02 '15 at 19:00
  • Notepad can save files in 4 different encodings. When you click *File > Save As...* the encoding of the current file is displayed in the bottom center of the dialog. – Ansgar Wiechers Jun 02 '15 at 20:02
  • 1
    You can also force the encoding in Powershell with `Get-Content input.txt | Set-Content output.txt -Encoding Ascii` (or Utf8 or whichever) – Ryan Bemrose Jun 02 '15 at 22:06

2 Answers2

0

Try opening your batch file in Notepad, and then 'Save As...' and be sure to select the ANSI encoding. Then don't touch it in your other editor :D.

It seems related to this issue.

I ran into this trying to write a batch file in Atom, and I think it was automatically encoding it to UTF-8. Opening in Notepad and re-saving it using ANSI encoding solved it for me.

A complicating issue for me was that my script was switching back and forth between git branches during my batch file, and if the batch file was different between the branches it would start to execute only partial lines as the computer would try to pick up the next piece of the batch, and it was causing the same issue.

lionade
  • 25
  • 5
-2

From what I can see, the command line does not recognize the 'rshell' command.

Check your enviroment variables or start the command from where 'rshell'.com/.exe is located.

Other than this there is not much to say without seeing the content of the powershell script...

rghome
  • 8,529
  • 8
  • 43
  • 62
  • 2
    Obviously it's supposed to be `powershell`, but somthing is eating up the first 4 characters of the second command from his script. That is what his question is about. – Ansgar Wiechers Jun 02 '15 at 17:55
  • Correct. The command in the bat file is 'powershell "C:\SetupVM.ps1"' But for some reason it's losing the first 4 characters. – Gavin Greenwalt Jun 02 '15 at 17:59