From 72979eb3f3e9461aeb4acce49eb3300860ff73d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=81=C4=85czykowski?= Date: Fri, 14 Jun 2024 15:57:35 +0200 Subject: [PATCH] seperate windows update and winget py --- update-pc-script-auto.py | 2 ++ update-pc-script.py | 2 ++ utilities.py | 30 +----------------------------- windows_update.py | 21 +++++++++++++++++++++ winget.py | 9 +++++++++ 5 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 windows_update.py create mode 100644 winget.py diff --git a/update-pc-script-auto.py b/update-pc-script-auto.py index c19965c..a93cc08 100644 --- a/update-pc-script-auto.py +++ b/update-pc-script-auto.py @@ -2,6 +2,8 @@ from utilities import * from motherboard import * from chocolatey import * +from winget import * +from windows_update import * if __name__ == "__main__": if run_as_admin(): diff --git a/update-pc-script.py b/update-pc-script.py index 9f56bb5..32e8d0e 100644 --- a/update-pc-script.py +++ b/update-pc-script.py @@ -1,6 +1,8 @@ from motherboard import * from utilities import * from chocolatey import * +from winget import * +from windows_update import * if __name__ == "__main__": if run_as_admin(): diff --git a/utilities.py b/utilities.py index 101aa87..ebc9096 100644 --- a/utilities.py +++ b/utilities.py @@ -74,32 +74,4 @@ def install_program(installer_path, program_name): except subprocess.CalledProcessError as e: print(f"Error installing {program_name}: {e}") else: - print("Error: Installer not found.") - -def winget_upgrade(): - # Upgrade all installed packages using winget - try: - subprocess.run(["winget", "upgrade", "--all", "--accept-source-agreements", "--accept-package-agreements", "--allow-reboot"], check=True,) - print("All installed packages upgraded successfully.") - except subprocess.CalledProcessError as e: - print(f"Error upgrading packages with winget: {e}") - -def windows_update(): - try: - print("Checking for Windows updates...") - - # Install the PSWindowsUpdate module if not already installed - ps_install_command = ( - "Install-Module PSWindowsUpdate -Force -AllowClobber -Scope AllUsers; " - "Set-ExecutionPolicy Bypass -Scope Process -Force; " - "Import-Module PSWindowsUpdate -Force;" - "$serviceManager = New-Object -ComObject 'Microsoft.Update.ServiceManager';" - "$serviceManager.AddService2('7971f918-a847-4430-9279-4a52d1efe18d',7,'');" - "Install-WindowsUpdate -AcceptAll -AutoReboot -IgnoreUserInput;" - ) - - subprocess.run(["powershell", "-ExecutionPolicy", "Bypass", "-Command", ps_install_command], check=True) - - print("Windows updates checked and installed successfully.") - except subprocess.CalledProcessError as e: - print(f"Error checking or installing Windows updates: {e}") + print("Error: Installer not found.") \ No newline at end of file diff --git a/windows_update.py b/windows_update.py new file mode 100644 index 0000000..4613103 --- /dev/null +++ b/windows_update.py @@ -0,0 +1,21 @@ +import subprocess + +def windows_update(): + try: + print("Checking for Windows updates...") + + # Install the PSWindowsUpdate module if not already installed + ps_install_command = ( + "Install-Module PSWindowsUpdate -Force -AllowClobber -Scope AllUsers; " + "Set-ExecutionPolicy Bypass -Scope Process -Force; " + "Import-Module PSWindowsUpdate -Force;" + "$serviceManager = New-Object -ComObject 'Microsoft.Update.ServiceManager';" + "$serviceManager.AddService2('7971f918-a847-4430-9279-4a52d1efe18d',7,'');" + "Install-WindowsUpdate -AcceptAll -AutoReboot -IgnoreUserInput;" + ) + + subprocess.run(["powershell", "-ExecutionPolicy", "Bypass", "-Command", ps_install_command], check=True) + + print("Windows updates checked and installed successfully.") + except subprocess.CalledProcessError as e: + print(f"Error checking or installing Windows updates: {e}") diff --git a/winget.py b/winget.py new file mode 100644 index 0000000..98dfd99 --- /dev/null +++ b/winget.py @@ -0,0 +1,9 @@ +import subprocess + +def winget_upgrade(): + # Upgrade all installed packages using winget + try: + subprocess.run(["winget", "upgrade", "--all", "--accept-source-agreements", "--accept-package-agreements", "--allow-reboot"], check=True,) + print("All installed packages upgraded successfully.") + except subprocess.CalledProcessError as e: + print(f"Error upgrading packages with winget: {e}")