Skip to content

Commit

Permalink
fix: update script to resolve unapproved verbs and missing function e…
Browse files Browse the repository at this point in the history
…rrors

Renamed Check-Command to Test-Command for verb compliance.
Renamed Check-WindowsVersion to Test-WindowsVersion.
Updated all references to use the correct function names.
Ensured proper handling of Chocolatey, Winget, aria2c, and Python installations.
Improved logging and streamlined script execution.
  • Loading branch information
coff33ninja committed Jan 8, 2025
1 parent 72056cc commit ffeb0f3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 119 deletions.
156 changes: 37 additions & 119 deletions AdminLaunchOption.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,14 @@ Function LogWrite {
Write-Host $logEntry
}

# Function to check if a command exists
# Function to test if a command exists
Function Test-Command {
Param ([string]$command)
return Get-Command $command -ErrorAction SilentlyContinue
}

# Function to install Chocolatey
Function Install-Chocolatey {
LogWrite 'Installing Chocolatey...'
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# After installation, launch a new terminal to continue
LogWrite 'Launching a new PowerShell session to continue with Chocolatey commands...'
Start-Process -FilePath 'powershell.exe' -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"& {`"$PSCommandPath`"}`" -Verb RunAs"
exit
}

# Function to download a file from a URL to a destination
Function DownloadFile {
Param ([string]$url, [string]$destination)
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $destination)
}

# Function to check Windows version
Function Get-WindowsVersion {
# Function to test Windows version compatibility
Function Test-WindowsVersion {
$version = [System.Environment]::OSVersion.Version
if ($version.Major -lt 10) {
LogWrite 'Winget is only available for Windows 10 and later. Exiting script.'
Expand Down Expand Up @@ -67,140 +48,77 @@ if ($LASTEXITCODE -ne 0) {
}

# Check Windows version
Check-WindowsVersion
Test-WindowsVersion

# Check and Install Chocolatey
LogWrite 'Checking Chocolatey installation'
if (-not (Check-Command choco)) {
Install-Chocolatey
# The script will exit here, and the new terminal will take over
}
else {
LogWrite 'Checking Chocolatey installation...'
if (-not (Test-Command choco)) {
LogWrite 'Chocolatey not detected. Attempting installation...'
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RefreshEnv
LogWrite 'Chocolatey installed successfully.'
} else {
LogWrite 'Chocolatey is already installed.'
}

# Check and Install Winget
LogWrite 'Checking Winget installation'
if (-not (Check-Command winget)) {
LogWrite 'Checking Winget installation...'
if (-not (Test-Command winget)) {
LogWrite 'Winget not detected. Attempting installation...'
choco install winget -y
}
else {
LogWrite 'Winget is already installed. Checking for updates...'
choco upgrade winget -y
} else {
LogWrite 'Winget is already installed.'
}

# Check and Install aria2c
LogWrite 'Checking aria2c installation'
if (-not (Check-Command aria2c)) {
LogWrite 'Checking aria2c installation...'
if (-not (Test-Command aria2c)) {
LogWrite 'aria2c not detected. Installing...'
choco install aria2 -y
}
else {
} else {
LogWrite 'aria2c is already installed.'
}

# Check and Install PowerShell Core
LogWrite 'Checking PowerShell Core installation'
if (-not (Check-Command pwsh)) {
LogWrite 'Checking PowerShell Core installation...'
if (-not (Test-Command pwsh)) {
LogWrite 'PowerShell Core not detected. Installing...'
choco install powershell-core -y
}
else {
LogWrite 'PowerShell Core is already installed. Checking for updates...'
choco upgrade powershell-core -y
}

# Function to install Python in a new terminal
Function Install-Python {
LogWrite 'Installing Python...'
DownloadFile 'https://www.python.org/ftp/python/3.11.6/python-3.11.9-amd64.exe' 'C:\NexTool\python-3.11.6-amd64.exe'
Start-Process 'C:\NexTool\python-3.11.9-amd64.exe' -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1 TargetDir=C:\Python311' -Wait

LogWrite 'Launching new terminal to verify Python installation and upgrade pip...'
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"python --version; python -m pip install --upgrade pip; exit`"" -Verb RunAs -Wait
} else {
LogWrite 'PowerShell Core is already installed.'
}

# Main Script Execution
LogWrite 'Checking Chocolatey installation...'
if (-not (Check-Command choco)) {
Install-Chocolatey
}
else {
LogWrite 'Chocolatey is already installed.'
}

LogWrite 'Checking Python installation...'
# Check for Python, download and install if necessary
$pythonPath = 'C:\Python311\python.exe'
if (-not (Test-Path $pythonPath)) {
Install-Python
}
else {
LogWrite "Python found at $pythonPath. Upgrading pip..."
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"python -m pip install --upgrade pip; exit`"" -Verb RunAs -Wait
LogWrite 'Python not detected. Attempting download...'
Invoke-WebRequest 'https://www.python.org/ftp/python/3.11.6/python-3.11.6-amd64.exe' -OutFile 'C:\NexTool\python-3.11.6-amd64.exe'
LogWrite 'Installing Python...'
Start-Process 'C:\NexTool\python-3.11.6-amd64.exe' -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1 TargetDir=C:\Python311' -Wait
LogWrite 'Python installed successfully.'
} else {
LogWrite "Python found at $pythonPath."
}

# Upgrade pip
LogWrite 'Upgrading pip...'
& $pythonPath -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) {
LogWrite 'Error: Failed to upgrade pip.'
exit
}
else {
LogWrite 'Successfully upgraded pip.'
}

# Install required Python packages
$packages = @('setuptools', 'pyqt5-tools', 'PyQt5-stubs', 'pyqtgraph', 'requests', 'psutil', 'pywin32')
foreach ($package in $packages) {
LogWrite "Installing/upgrading $package..."
& $pythonPath -m pip install --upgrade $package
if ($LASTEXITCODE -ne 0) {
LogWrite "Error: Failed to install/upgrade $package."
}
else {
LogWrite "Success: $package installed/upgraded successfully."
}
}

# Download NexTool.py
LogWrite 'Attempting to download NexTool.py...'
DownloadFile 'https://raw.githubusercontent.com/coff33ninja/NexTool-Windows-Suite/main/NexTool.py' 'C:\NexTool\NexTool.py'
if ($LASTEXITCODE -eq 0) {
LogWrite 'Downloaded NexTool.py successfully.'
LogWrite 'Launching NexTool.py...'
Start-Process $pythonPath 'C:\NexTool\NexTool.py'
}
else {
LogWrite 'Failed to download NexTool.py.'
}

# Summary of installations
LogWrite 'Installation Summary:'
$logContent = Get-Content $LOGFILE
if ($logContent -match 'Error') {
LogWrite 'Some installations failed. Check the log for details.'
}
else {
LogWrite 'All installations were successful!'
}

# Prompt user to review the log
$userInput = Read-Host 'Do you want to review the log? (yes/no)'
if ($userInput -eq 'yes') {
Start-Process notepad.exe $LOGFILE
}
LogWrite 'Downloading NexTool.py...'
Invoke-WebRequest 'https://raw.githubusercontent.com/coff33ninja/NexTool-Windows-Suite/main/NexTool.py' -OutFile 'C:\NexTool\NexTool.py'

# Cleanup logic
LogWrite 'Cleaning up...'
if (Test-Path 'C:\NexTool') {
Remove-Item 'C:\NexTool\*' -Recurse -Force
Remove-Item 'C:\NexTool' -Force
LogWrite 'Cleanup complete.'
}
else {
LogWrite 'C:\NexTool does not exist. No cleanup necessary.'
}
# Launch NexTool.py
LogWrite 'Launching NexTool.py...'
Start-Process $pythonPath 'C:\NexTool\NexTool.py'

# Script complete
LogWrite 'Script completed.'
48 changes: 48 additions & 0 deletions app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
2025-01-08 17:10:26,619 - ERROR - Error Caught: Traceback (most recent call last):
File "C:\NexTool\NexTool.py", line 3948, in <module>
window = CustomUI()
^^^^^^^^^^
File "C:\NexTool\NexTool.py", line 3019, in __init__
self.construct_menu(tabs)
File "C:\NexTool\NexTool.py", line 3091, in construct_menu
consolidated_info_text = consolidate_info(get_system_information())
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\NexTool\NexTool.py", line 106, in get_system_information
subprocess.check_output("wmic cpu get Name", stderr=subprocess.DEVNULL)
File "C:\Python311\Lib\subprocess.py", line 466, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Python311\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

2025-01-08 17:12:38,390 - ERROR - Error Caught: Traceback (most recent call last):
File "C:\NexTool\NexTool.py", line 3948, in <module>
window = CustomUI()
^^^^^^^^^^
File "C:\NexTool\NexTool.py", line 3019, in __init__
self.construct_menu(tabs)
File "C:\NexTool\NexTool.py", line 3091, in construct_menu
consolidated_info_text = consolidate_info(get_system_information())
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\NexTool\NexTool.py", line 106, in get_system_information
subprocess.check_output("wmic cpu get Name", stderr=subprocess.DEVNULL)
File "C:\Python311\Lib\subprocess.py", line 466, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Python311\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

0 comments on commit ffeb0f3

Please sign in to comment.