Skip to content

Commit 7e0d960

Browse files
author
Jonathan Held
committed
Create install-python.vbs
Initial vbs script to install Python.
1 parent 0270779 commit 7e0d960

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

install-python.vbs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
' Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
'
3+
' Licensed under the Apache License, Version 2.0 (the "License"). You
4+
' may not use this file except in compliance with the License. A copy of
5+
' the License is located at
6+
'
7+
' http://aws.amazon.com/apache2.0/
8+
'
9+
' or in the "license" file accompanying this file. This file is
10+
' distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
' ANY KIND, either express or implied. See the License for the specific
12+
' language governing permissions and limitations under the License.
13+
14+
' Python installation script for Windows.
15+
' To run this script, type "cscript install-python.vbs && exit" from the command line.
16+
17+
dim fso: set fso = CreateObject ("Scripting.FileSystemObject")
18+
dim stdout: set stdout = fso.GetStandardStream(1)
19+
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
20+
21+
if NOT(isPythonInstalled()) then
22+
downloadUrl = pythonDownload()
23+
stdout.WriteLine "Downloading Python from " + downloadUrl
24+
if len(downloadUrl) > 0 then
25+
if (installPython() = 0) then
26+
openNewCommandWindow()
27+
WScript.Quit 0
28+
end if
29+
else
30+
WScript.Quit -1
31+
end if
32+
end if
33+
34+
set stdout = Nothing
35+
set fso = Nothing
36+
37+
function installPython()
38+
dim http: set http = createobject("Microsoft.XMLHTTP")
39+
dim stream: set stream = createobject("Adodb.Stream")
40+
http.Open "GET", downloadUrl, False
41+
http.Send
42+
43+
with stream
44+
.type = 1 'binary
45+
.open
46+
.write http.responseBody
47+
.savetofile "python3.7.3.exe", 2 'overwrite
48+
end with
49+
stream.Flush()
50+
stdout.WriteLine "Download complete."
51+
52+
set stream = Nothing
53+
set http = Nothing
54+
55+
dim shell: set shell = CreateObject("WScript.Shell")
56+
stdout.WriteLine "Silently installing Python. Do not close this window."
57+
exitCode = shell.Run("python3.7.3.exe " + "/quiet InstallAllUsers=1 PrependPath=1",,true) 'Wait on return
58+
if (exitCode = 0) then
59+
stdout.WriteLine "Installation completed successfully."
60+
else
61+
stdout.WriteLine "Installation failed with exit code " & exitCode
62+
end if
63+
64+
if (fso.FileExists(currentDirectory & "python3.7.3.exe")) then
65+
fso.DeleteFile(currentDirectory & "python3.7.3.exe")
66+
end if
67+
68+
installPython = exitCode
69+
70+
set shell = Nothing
71+
72+
end function
73+
74+
function isPythonInstalled()
75+
dim wmi: set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
76+
dim query: set query = wmi.ExecQuery ("Select * from Win32_Product where Caption Like '%Python 3.7.3 Executables%'")
77+
isPythonInstalled = (query.Count <> 0)
78+
if (isPythonInstalled) then
79+
stdout.WriteLine "Found an existing Python installation."
80+
end if
81+
set query = Nothing
82+
set wmi = Nothing
83+
end function
84+
85+
function openNewCommandWindow()
86+
dim wmi: set wmi = GetObject("winmgmts:\\.\root\cimv2")
87+
dim config: set config = wmi.Get("Win32_ProcessStartup")
88+
config.SpawnInstance_
89+
config.X = 100
90+
config.Y = 100
91+
92+
dim commandProcess: set commandProcess = wmi.Get("Win32_Process")
93+
commandProcess.Create "cmd.exe", currentDirectory, config, procId
94+
95+
set config = Nothing
96+
set wmi = Nothing
97+
end function
98+
99+
function pythonDownload()
100+
dim shell : set shell = CreateObject("WScript.Shell")
101+
dim process: set process = shell.Environment("Process")
102+
103+
processor = process("PROCESSOR_ARCHITECTURE")
104+
select case LCase(processor)
105+
case "x86"
106+
stdout.WriteLine "Downloading x86 version of Python."
107+
pythonDownload = "https://www.python.org/ftp/python/3.7.3/python-3.7.3-webinstall.exe"
108+
case "amd64"
109+
stdout.WriteLine "Downloading x64 version of Python."
110+
pythonDownload = "https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64-webinstall.exe"
111+
case else
112+
stdout.WriteLine "Unable to determine the Python download url for processor type: " & processor
113+
pythonDownload = ""
114+
end select
115+
116+
set process = Nothing
117+
set shell = Nothing
118+
end function

0 commit comments

Comments
 (0)