嗯,这是我的batch
脚本:
@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
首先,它必须最小化command prompt
,这实际上是有效的。但它也必须:
PowerShell 3.0
,如果没有,请下载和
安装它PowerShell
时,下载图片.cs
文件在我的计算机中,批处理运行完美,但在任何其他计算机中,只是不工作。 你能告诉我什么错了吗?
(我不能流利地说英语,所以请原谅我任何语言错误)
答案 0 :(得分:1)
试试这段代码:
@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"
该行
if %internet% == "Nao Conectado" (GOTO Rodando)
如果internet
的值为Nao Conectado
,则导致执行时出现语法错误,因为值的空间并且在等式条件的左侧没有使用双引号。即使值Conectado
,条件也始终为FALSE,因为不带双引号的字符串与具有双引号的字符串区分大小写。
并始终使用set "variable=value"
,原因在于答案
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
在最后两行中添加双引号,因为批处理文件的路径可能包含一个或多个空格。
我没有检查创建* .cs文件的块,运行安装程序和运行PowerShell脚本。