简体   繁体   English

我的脚本可以在我的计算机上正常运行,但不能在另一台计算机上运行

[英]My script work perfectly on my computer, but not on another computer

Well, this is my batch script: 好吧,这是我的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

First of all, it must run the command prompt minimized, and thats actually working. 首先,它必须在最小化的情况下运行command prompt ,这才是切实可行的。 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 检查是否已安装PowerShell 3.0 ,如果未安装,请下载并安装
  3. When PowerShell is installed, download an image 安装PowerShell ,下载映像
  4. Delete the .cs file 删除.cs文件
  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) (我不会说流利的英语,所以请您原谅我的语言错误)

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. 如果internet值为Nao Conectado导致执行时出现语法错误,这是因为值的空间Nao Conectado ,并且方程式条件的左侧未使用双引号。 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. 即使使用值Conectado ,条件也始终为FALSE,因为将不带双引号的字符串与区分大小写的字符串与带双引号的字符串进行比较。

And use always set "variable=value" for the reasons explained in answer on 并使用始终set "variable=value"的原因是因为
Why is no string output with 'echo %var%' after using 'set var = text' on command line? 为什么在命令行上使用'set var = text'后没有显示'echo%var%'的字符串?

On the last two lines double quotes are added as path to batch file could contain 1 or more spaces. 最后两行添加双引号,因为批处理文件的路径可能包含1个或多个空格。

I have not checked the block creating the *.cs file, running installer and running PowerShell script. 我尚未检查创建* .cs文件,运行安装程序和运行PowerShell脚本的块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个exe脚本到select并移动我电脑上的所有图像 - Create an exe script to select and move all images on my computer 如何在本地网络上找到另一台计算机或服务器的公共IP? - How to find the public ip of another computer or server on my local network? PowerShell从远程计算机上我的计算机上的位置运行bat - PowerShell run bat from location on my computer on remote computer Powershell 将计算机调用到在我的本地计算机重新启动后仍然存在的远程计算机? - Powershell invoke-computer to a remote that survives a reboot of my local computer? Powershell - 如何运行位于另一台计算机上的脚本? - Powershell - how to run a script located on another computer? 将有关我的计算机硬件的信息发送到数据库 - Send information about hardware of my computer to a database csv中每台计算机的开始工作 - Start-job for each computer in my csv 映射的网络驱动器未显示在“我的电脑”中 - Mapped network drives are not showing in My Computer 如何检查域中是否已存在计算机名称我在每台电脑中使用的脚本 - How to check if computer name is already exist in the domain My script used in each pc 有没有一种方法可以警告远程计算机等待重新启动,并且让我的脚本等待重新启动完成? - Is there a way to warn remote computer of pending restart AND have my script wait for the restart to complete?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM