Clint Eastwood
Clint Eastwood

Reputation: 135

How to seperate lines of .bat when encrypted from powershell

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)

Upvotes: 0

Views: 246

Answers (3)

oɔɯǝɹ
oɔɯǝɹ

Reputation: 7625

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

Upvotes: 1

rojo
rojo

Reputation: 24476

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

Upvotes: 1

Leptonator
Leptonator

Reputation: 3519

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!

Upvotes: 0

Related Questions