Python253

default_console_configurations

Apr 27th, 2024 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: default_console_configurations.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script configures properties of both Command Prompt and PowerShell consoles based on the user's privileges.
  9. For Command Prompt, it sets the window size, font, QuickEdit mode, and text color.
  10. For PowerShell, it sets similar properties.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.    - Windows operating system
  15.  
  16. Functions:
  17.    is_admin():
  18.        Checks if the script is running with administrative privileges.
  19.        Returns:
  20.            bool: True if the script is running as administrator, False otherwise.
  21.  
  22.    set_console_registry_value(key_path, value_name, value_data, value_type):
  23.        Sets a registry value for a given key path.
  24.        Parameters:
  25.            key_path (str): The full path to the registry key.
  26.            value_name (str): The name of the registry value.
  27.            value_data (int): The data to set for the registry value.
  28.            value_type (int): The type of the registry value.
  29.        Returns:
  30.            str: Message indicating whether the operation was successful or not.
  31.  
  32.    get_text_color():
  33.        Determines the appropriate text color based on the user's privilege level.
  34.        Returns:
  35.            int: The color code for the text (0-15).
  36.  
  37. Usage:
  38.    Run the script in a Python environment. It automatically adjusts console properties based on the user's privilege level.
  39.  
  40. Additional Notes:
  41.    - For Command Prompt, green text is used for the administrator console, while white text is used for a standard terminal.
  42.    - The script sets properties for both Command Prompt and PowerShell consoles.
  43. """
  44.  
  45. import winreg
  46. import ctypes
  47.  
  48. def is_admin():
  49.     try:
  50.         return ctypes.windll.shell32.IsUserAnAdmin()
  51.     except:
  52.         return False
  53.  
  54. def set_console_registry_value(key_path, value_name, value_data, value_type):
  55.     full_key_path = r'HKEY_CURRENT_USER\Console\{}'.format(key_path)
  56.     try:
  57.         # Open the existing key
  58.         key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, full_key_path, 0, winreg.KEY_WRITE)
  59.     except FileNotFoundError:
  60.         return f"\nRESULT: ---> ---> ---> FAILURE!\nKey Path does not exist:\n{full_key_path.strip()}"
  61.     try:
  62.         # Set the registry value
  63.         winreg.SetValueEx(key, value_name, 0, value_type, value_data)
  64.         return f"\nRESULT: ---> ---> ---> SUCCESS!\nKey Path:\n{full_key_path.strip()}\nName: {value_name.strip()}\nValue: {value_data}"
  65.     except Exception as e:
  66.         return f"\nRESULT: ---> ---> ---> FAILURE!\nKey Path:\n{full_key_path.strip()}\nName: {value_name.strip()}\nValue: {value_data}\nError: {e}"
  67.     finally:
  68.         # Close the registry key
  69.         winreg.CloseKey(key)
  70.  
  71. def get_text_color():
  72.     if is_admin():
  73.         # Green text for administrator console
  74.         return 2
  75.     else:
  76.         # White text for standard terminal
  77.         return 15
  78.  
  79. # Set properties for Command Prompt
  80. print("\nSetting properties for Command Prompt...")
  81. cmd_reg_key = r'%SystemRoot%_system32_cmd.exe'
  82. cmd_width = 180
  83. cmd_window_height = 75
  84. cmd_buffer_height = 9999
  85. cmd_screen_buffer_size = cmd_buffer_height + cmd_width * 0x10000
  86. cmd_window_size = cmd_window_height + cmd_width * 0x10000
  87. print(set_console_registry_value(cmd_reg_key, "ScreenBufferSize", cmd_screen_buffer_size, winreg.REG_DWORD))
  88. print(set_console_registry_value(cmd_reg_key, "WindowSize", cmd_window_size, winreg.REG_DWORD))
  89. cmd_font_reg_key = r'%SystemRoot%_system32_cmd.exe'
  90. cmd_font_name = 'Consolas'
  91. cmd_font_size = 12 * 0x10000
  92. print(set_console_registry_value(cmd_font_reg_key, "FontSize", cmd_font_size, winreg.REG_DWORD))
  93. print(set_console_registry_value(cmd_font_reg_key, "FaceName", cmd_font_name, winreg.REG_SZ))
  94. print(set_console_registry_value(cmd_font_reg_key, "FontWeight", 400, winreg.REG_DWORD))
  95. cmd_quick_edit_reg_key = r'%SystemRoot%_system32_cmd.exe'
  96. print(set_console_registry_value(cmd_quick_edit_reg_key, "QuickEdit", 1, winreg.REG_DWORD))
  97. cmd_color_reg_key = r'%SystemRoot%_system32_cmd.exe'
  98. fg = get_text_color()
  99. bg = 0
  100. cmd_color = 16 * bg + fg
  101. print(set_console_registry_value(cmd_color_reg_key, "ScreenColors", cmd_color, winreg.REG_DWORD))
  102.  
  103. # Set properties for PowerShell
  104. print("\nSetting properties for PowerShell...")
  105. ps_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
  106. ps_width = 180
  107. ps_window_height = 75
  108. ps_buffer_height = 9999
  109. ps_screen_buffer_size = ps_buffer_height + ps_width * 0x10000
  110. ps_window_size = ps_window_height + ps_width * 0x10000
  111. print(set_console_registry_value(ps_reg_key, "ScreenBufferSize", ps_screen_buffer_size, winreg.REG_DWORD))
  112. print(set_console_registry_value(ps_reg_key, "WindowSize", ps_window_size, winreg.REG_DWORD))
  113. ps_font_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
  114. ps_font_name = 'Consolas'
  115. ps_font_size = 12 * 0x10000
  116. print(set_console_registry_value(ps_font_reg_key, "FontSize", ps_font_size, winreg.REG_DWORD))
  117. print(set_console_registry_value(ps_font_reg_key, "FaceName", ps_font_name, winreg.REG_SZ))
  118. print(set_console_registry_value(ps_font_reg_key, "FontWeight", 400, winreg.REG_DWORD))
  119. ps_quick_edit_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
  120. print(set_console_registry_value(ps_quick_edit_reg_key, "QuickEdit", 1, winreg.REG_DWORD))
  121. ps_color_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
  122. fg = get_text_color()
  123. bg = 0
  124. ps_color = 16 * bg + fg
  125. print(set_console_registry_value(ps_color_reg_key, "ScreenColors", ps_color, winreg.REG_DWORD))
  126.  
  127.  
Add Comment
Please, Sign In to add comment