0

I have a 10KB powershell script that I am trying to convert to a .bat. The format of the script is 1. declare constants 2. a bunch of for each child item loops involving the sending of an email and the moving of a file. Other bat files that I have made do work.

There are 6 non-nested loops. When the bat of each loop is run in isolation (with the declarations), everything works. When I combine 3, one group works, the other sends the email without moving the file. When all are combined, nothing works and command prompt window doesn't even pop up.

I read that the line length limit of a bat file is 127 bytes (http://support.microsoft.com/kb/69563) and I think that this is the cause of my problem. Do I need to do something in powershell to separate the lines so the bat file won't try to read it as one line?

To convert, I used the script from here (http://www.altitudeintegration.com/PowerShellToBatFile.aspx)

Clint Eastwood
  • 135
  • 1
  • 7
  • 1
    You'll notice that the "applies to" section on that page are all DOS versions, not `CMD`. I believe that there is no 127-byte limit under `CMD`. Unlikely that *that* alone is causing the problem. Try turning `echo` on at a place you know the procedure is reaching, then add a `pause` statement at another point after your problem section. This will restrict the amount of diagnostic displayed and may assist you in tracing the problem. – Magoo Jan 02 '15 at 17:57
  • What do you actually mean with `encrypted` in the title? Don't you mean `converted`? – oɔɯǝɹ Jan 12 '15 at 19:26

3 Answers3

1

You can call the original PowerShell script from a batch file using

powershell.exe -ExecutionPolicy RemoteSigned -File myscript.ps1

This way you can use your script by doubleclicking on the batch file.

The ExecutionPolicy parameter can be used to allow the script to run, without altering the machine level ExecutionPolicy:

Sets the default execution policy for the current session and saves it in the $env:PSExecutionPolicyPreference environment variable. This parameter does not change the Windows PowerShell execution policy that is set in the registry.

This way there is no need to convert the PowerShell scripts to batch (which may not be possible at all).

You can use powershell.exe /? from a command prompt to discover all it's options.

Note that PowerShell defaults to setting the ExecutionPolicy to Restricted (RemoteSigned in Windows Server 2012 R2). Changing the machine wide policy in machine scope requires local admin rights.

Using the -ExecutionPolicy parameter on powershell.exe sets the executionpolicy for the local scope only.

See more details on ExecutionPolicy using

Get-Help about_Execution_Policies

or look here http://technet.microsoft.com/en-US/library/hh847748.aspx

You can find the PowerShell.exe executable in

C:\Windows\System32\WindowsPowerShell\v1.0\powersell.exe

(see: Path to Powershell.exe (v 2.0) )

Community
  • 1
  • 1
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
  • Unfortunately, the computer has a high security policy and I cannot run a saved PS script; I can only select the code in the ISE and run the selection. – Clint Eastwood Jan 02 '15 at 18:23
  • @ClintEastwood That seems a rather shortsighted security policy to me. What harm could a PowerShell script do, that a .bat file or executable cannnot? – oɔɯǝɹ Jan 02 '15 at 19:04
  • Its not that they specifically disabled ps scripts. Don't off the shelf computers come with running ps scripts disabled until you change the permissions? I can't change the permissions away from the default because I am not the admin. – Clint Eastwood Jan 02 '15 at 19:28
  • It was helpful and I can run some scripts like this, but this script is not working when called by a bat. – Clint Eastwood Jan 04 '15 at 00:57
  • 1
    Of the answers, this was the best because it tells me how I need to phrase my next question, which I will do when I get home. – Clint Eastwood Jan 04 '15 at 21:23
1

This is not the entire answer to your question, but I specifically wanted to address your 127-byte assertion. That may have been true for 16-bit DOS, but cmd.exe has no such limitation.

test case:

@echo off
setlocal

set "longline=############################################################################################################################################################################################################################################################################################################"

call :length %longline% len

echo longline is %len% bytes

:: end main script
goto :EOF

:length <string> <var_to_set>
setlocal enabledelayedexpansion
set "str=%~1"
for /l %%I in (1,1,1000) do (
    if "!str!"=="!str:~-%%I!" (
        endlocal && set "%~2=%%I"
        goto :EOF
    )
)

output:

longline is 300 bytes

rojo
  • 24,000
  • 5
  • 55
  • 101
0

Did you originally script or code the bat file?

If not, can you pull that file out of backup?

I know we used to encode our KiX Scripts, so you should have the magic decoder ring as it were. Personally, I would be very careful in encoding your scripts unless they include login credentials. Other than that, I would leave them as raw text.

Thanks!

Leptonator
  • 3,379
  • 2
  • 38
  • 51
  • I started with a powershell script (which works perfectly) but I needed it to be something one can click on because there are high security settings on the the computer and I can't run saved ps scripts by clicking on them. I used a bat maker I found online – Clint Eastwood Jan 02 '15 at 18:52
  • 1
    If the security settings of your environment prevent you from getting work done, then those settings are wrong (for lack of a better word). PowerShell is no more a security risk than `BAT` files and with script signing available to validate that scripts haven't been tampered with, can be *more* secure. – alroc Jan 02 '15 at 18:54
  • Preachin' to the choir – Clint Eastwood Jan 02 '15 at 18:57