I'm new to python, I'm trying to use a script to automatically install python from Powershell, the following:
# This is the link to download Python 3.6.7 from Python.org
# See https://www.python.org/downloads/
$pythonUrl = "https://www.python.org/ftp/python/3.6.7/python-3.6.7-amd64.exe"
# This is the directory that the exe is downloaded to
$tempDirectory = "C:\temp_provision\"
# Installation Directory
# Some packages look for Python here
$targetDir = "C:\Python36"
# create the download directory and get the exe file
$pythonNameLoc = $tempDirectory "python367.exe"
New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($pythonUrl, $pythonNameLoc)
# These are the silent arguments for the install of python
# See https://docs.python.org/3/using/windows.html
$Arguments = @()
$Arguments = "/i"
$Arguments = 'InstallAllUsers="1"'
$Arguments = 'TargetDir="' $targetDir '"'
$Arguments = 'DefaultAllUsersTargetDir="' $targetDir '"'
$Arguments = 'AssociateFiles="1"'
$Arguments = 'PrependPath="1"'
$Arguments = 'Include_doc="1"'
$Arguments = 'Include_debug="1"'
$Arguments = 'Include_dev="1"'
$Arguments = 'Include_exe="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'InstallLauncherAllUsers="1"'
$Arguments = 'Include_lib="1"'
$Arguments = 'Include_pip="1"'
$Arguments = 'Include_symbols="1"'
$Arguments = 'Include_tcltk="1"'
$Arguments = 'Include_test="1"'
$Arguments = 'Include_tools="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = 'Include_launcher="1"'
$Arguments = "/passive"
#Install Python
Start-Process $pythonNameLoc -ArgumentList $Arguments -Wait
Function Get-EnvVariableNameList {
[cmdletbinding()]
$allEnvVars = Get-ChildItem Env:
$allEnvNamesArray = $allEnvVars.Name
$pathEnvNamesList = New-Object System.Collections.ArrayList
$pathEnvNamesList.AddRange($allEnvNamesArray)
return ,$pathEnvNamesList
}
Function Add-EnvVarIfNotPresent {
Param (
[string]$variableNameToAdd,
[string]$variableValueToAdd
)
$nameList = Get-EnvVariableNameList
$alreadyPresentCount = ($nameList | Where{$_ -like $variableNameToAdd}).Count
if ($alreadyPresentCount -eq 0)
{
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::User)
$message = "Enviromental variable added to machine, process and user to include $variableNameToAdd"
}
else
{
$message = 'Environmental variable already exists. Consider using a different function to modify it'
}
Write-Information $message
}
Function Get-EnvExtensionList {
[cmdletbinding()]
$pathExtArray = ($env:PATHEXT).Split("{;}")
$pathExtList = New-Object System.Collections.ArrayList
$pathExtList.AddRange($pathExtArray)
return ,$pathExtList
}
Function Add-EnvExtension {
Param (
[string]$pathExtToAdd
)
$pathList = Get-EnvExtensionList
$alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
if ($alreadyPresentCount -eq 0)
{
$pathList.Add($pathExtToAdd)
$returnPath = $pathList -join ";"
[System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::User)
$message = "Path extension added to machine, process and user paths to include $pathExtToAdd"
}
else
{
$message = 'Path extension already exists'
}
Write-Information $message
}
Function Get-EnvPathList {
[cmdletbinding()]
$pathArray = ($env:PATH).Split("{;}")
$pathList = New-Object System.Collections.ArrayList
$pathList.AddRange($pathArray)
return ,$pathList
}
Function Add-EnvPath {
Param (
[string]$pathToAdd
)
$pathList = Get-EnvPathList
$alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
if ($alreadyPresentCount -eq 0)
{
$pathList.Add($pathToAdd)
$returnPath = $pathList -join ";"
[System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::User)
$message = "Path added to machine, process and user paths to include $pathToAdd"
}
else
{
$message = 'Path already exists'
}
Write-Information $message
}
Add-EnvExtension '.PY'
Add-EnvExtension '.PYW'
Add-EnvPath 'C:\Python36\'
only that when I start it I get this pop-up:
how can i not display this window and start the default installation? Then is there a way to not show windows during installation? thank you very much for helping. Any advice on how to streamline the code is welcome
CodePudding user response:
You're seeing this window because you are not currently running in an elevated prompt. That is, you're not "admin" when you run the script. Therefore, the Python installer will prompt for elevation to administrator.
You can avoid the prompt by running in an elevated process. (right click "run as administrator").
On Windows, you can also use Start-Process -Verb RunAs
to elevate. However, you will still be prompted by the operating system to do this. The only way to not see that prompt when you run your script is for the script to be run for a process that is already elevated.