1

Well, this is my batch script:

@ECHO OFF
if not "%Minimizado%"=="" goto :Rodando
set Minimizado=true
start /min cmd /C "%~dpnx0"
goto :EOF

:Rodando
Ping www.google.nl -n 1 -w 1000
if errorlevel 1 (set internet=Nao Conectado) else (set internet=Conectado)
if %internet% == "Nao Conectado" (GOTO Rodando)
echo class Program { public static void Main() { >"%~dpn0.cs" echo using (var wc = new System.Net.WebClient()) { >>"%~dpn0.cs" echo wc.UseDefaultCredentials = true; >>"%~dpn0.cs" echo wc.DownloadFile(@"http://download.microsoft.com/download/1/1/7/117FB25C-BB2D-41E1-B01E-0FEB0BC72C30/WindowsServer2003-KB968930-x86-ENG.exe", @"%~dpn0.installer.exe");}}} >>"%~dpn0.cs" "%systemroot%\microsoft.net\framework\v3.5\csc.exe" /out:"%~dpn0.exe" "%~dpn0.cs" "%~dpn0.exe" "%~dpn0.installer.exe"
powershell.exe -Command "& {if($PSversionTable.PSVersion.Major -ge 3) {Invoke-WebRequest http://download1475.mediafire.com/dgmccvd5felg/cu6x9bzhx3hmz78/Teste.jpg -OutFile %~dpn0.png}}"
del %~dpn0.cs
START %~dpn0.png

First of all, it must run the command prompt minimized, and thats actually working. But it also must:

  1. Check if the internet connection is ok
  2. Check if PowerShell 3.0 is already installed, if don't, download and install it
  3. When PowerShell is installed, download an image
  4. Delete the .cs file
  5. Run the downloaded image

In my computer the batch runs perfectly, but in any other computer, simply don't work. Can you guys please tell me whats wrong?

(I don't speak english fluently, so please, forgive-me for any language mistake)

ArsonFG
  • 315
  • 8
  • 22
  • need a little more than "it doesn't work" some details about the type of errors you're getting or whatever – Scott Sosna Jan 27 '16 at 03:15
  • Console output would be helpful. Perhaps it's a Java issue? – BradzTech Jan 27 '16 at 03:26
  • @ScottSosna I don't have much more to say, the console simply closes itself and don't do anything that i listed in the question. The only thing to say is that the '.cs' file is created and deleted, but i don't know if it will help. ***(I don't speak english fluently, so please, forgive-me for any language mistake)*** – ArsonFG Jan 27 '16 at 03:47
  • instead of open the `.bat` file with double click, try open using cmd by navigating the location of the `.bat` file and open it, so you can see the error. – Tengku Fathullah Jan 27 '16 at 05:05

1 Answers1

1

Try this code:

@echo off
if not "%Minimizado%"=="" goto Rodando
set "Minimizado=true"
start /min cmd /C "%~f0"
goto :EOF

:Rodando
%SystemRoot%\System32\ping.exe www.google.nl -n 1 -w 1000
if errorlevel 1 goto Rodando
echo class Program { public static void Main() {>"%~dpn0.cs"
echo using (var wc = new System.Net.WebClient()) {>>"%~dpn0.cs"
echo wc.UseDefaultCredentials = true;>>"%~dpn0.cs"
echo wc.DownloadFile(@"http://download.microsoft.com/download/1/1/7/117FB25C-BB2D-41E1-B01E-0FEB0BC72C30/WindowsServer2003-KB968930-x86-ENG.exe", @"%~dpn0.installer.exe");}}}>>"%~dpn0.cs"
"%systemroot%\microsoft.net\framework\v3.5\csc.exe" /out:"%~dpn0.exe" "%~dpn0.cs"
"%~dpn0.exe"
"%~dpn0.installer.exe"
powershell.exe -Command "& {if($PSversionTable.PSVersion.Major -ge 3) {Invoke-WebRequest http://download1475.mediafire.com/dgmccvd5felg/cu6x9bzhx3hmz78/Teste.jpg -OutFile %~dpn0.png}}"
del "%~dpn0.cs"
start "%~dpn0.png"

The line

if %internet% == "Nao Conectado" (GOTO Rodando)

results in a syntax error on execution if value of internet is Nao Conectado because of space in value and no double quotes used on left side of equation condition. And even with value Conectado the condition is always FALSE as a string without double quotes is compared case-sensitive with a string with double quotes.

And use always set "variable=value" for the reasons explained in answer on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?

On the last two lines double quotes are added as path to batch file could contain 1 or more spaces.

I have not checked the block creating the *.cs file, running installer and running PowerShell script.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • SO would not let me edit your answer but the two commands for running `"%~dpn0.exe" "%~dpn0.installer.exe"` should each be on their own line. @DaMonki, I think the test for Minmizado will only work once because setlocal not present, but really should be a param on the 2nd invocation. the 1st exe does get created properly, but perhaps csc.exe is missing on the other machines. I am also not certain that link downloads powershell. It appears to download some `protectedroots registry key update` tool. Like other comments, output from your console and more echo/debug statements would be good. – Kory Gill Jan 27 '16 at 06:20
  • @KoryGill The block with `Minimizado` works as designed. On double clicking the batch file this variable is not defined with a value. Therefore the environment variable `Minimizado` is created with value `true`. Then a new command process is started with a copy of current environment variables and executed there is once again the batch file. The current command process ends and in new command process running with minimized window the variable `Minimizado` is defined and has value `true` and therefore now the batch file is really processing. – Mofi Jan 27 '16 at 06:35
  • Makes sense. I was thinking of running it from same command line multiple times. – Kory Gill Jan 27 '16 at 06:39
  • Well done my dear, it runs almost perfectly! (^-^). But... I need one last answer: Have Windows 8 *PowerShell 3.0* installed by default? (If ***Yes*** , i can cut at least 7 lines of my script, and i also may say sorry to you guys that waste your time in that 7 lines. But if ***Not*** , i will need some more help...) – ArsonFG Jan 27 '16 at 07:57
  • @DaMonki PowerShell 3.0 is installed and default on Windows 8.0 and PowerShell 4.0 is already default on Windows 8.1. – Mofi Jan 27 '16 at 17:54