Vbscript安装最新Java版本

时间:2014-01-08 17:36:26

标签: vbscript

我正在尝试自动安装Java以设置新计算机。 我计划将java下载到网络上的一个目录,并让脚本从那里获取安装。 我希望脚本能够在32位和64位安装程序之间进行确定,而不必在每次下载新安装程序时更改脚本。

如何使用通配符运行文件路径?

这是我到目前为止所拥有的

这决定了架构

Function GetArch
Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

'WScript.Echo "Running as a " & process_architecture & " process on a " & system_architecture & " system."
GetArch = system_architecture
End Function

这会运行可执行文件

Function runExec(strExec,blWait)
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell") 
objShell.Run strExec, 1 ,blWait
Set objShell = Nothing
End Function

这有望安装Java

Function InstallJava
if Instr(1, GetArch, "64") then
    runExec "\\fs1\IT\Scripts\Java\jre-*-x64.exe",true
    InstallJava = "Java 64bit Installed"
ElseIf Instr(1, GetArch, "86") then
    runExec "\\fs1\IT\Scripts\Java\jre-*-i586.exe",true
    InstallJava = "Java 32bit Installed"
End If
End Function

1 个答案:

答案 0 :(得分:2)

不支持通配符。您需要枚举文件,例如像这样:

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("\\fs1\IT\Scripts\Java").Files
  If LCase(Left(f.Name, 4)) = "jre-" Then
    If InStr(1, GetArch, "64") > 0 Then
      If LCase(Right(f.Name, 8)) = "-x64.exe" Then runExec f.Path, True
    ElseIf InStr(1, GetArch, "86") > 0 Then
      If LCase(Right(f.Name, 9)) = "-i586.exe" Then runExec f.Path, True
    End If
  End If
Next

但是,你可以使用不同的方法。由于您要下载并提供共享上的文件,您可以修改配置以创建/更新具有固定名称的符号链接到相应的文件,例如

D:\IT\Scripts\Java>mklink jre-CURRENT-x64.exe jre-7u45-windows-x64.exe
D:\IT\Scripts\Java>mklink jre-CURRENT-x86.exe jre-7u45-windows-i586.exe

然后在安装脚本中使用这些固定名称:

If Instr(1, GetArch, "64") > 0 then
  runExec "\\fs1\IT\Scripts\Java\jre-CURRENT-x64.exe", True
ElseIf Instr(1, GetArch, "86") > 0 then
  runExec "\\fs1\IT\Scripts\Java\jre-CURRENT-x86.exe", True
End If