-
Notifications
You must be signed in to change notification settings - Fork 2
/
Run-PrinterDiagnostics.py
112 lines (101 loc) · 5.24 KB
/
Run-PrinterDiagnostics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import subprocess
from time import sleep
import win32com.shell.shell as shell
import wmi
import base64
####### Variables ##########
activeUser = os.getlogin()
curUser = os.environ.get('USERNAME')
printers = subprocess.getoutput('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command "Get-Printer | Where name -notlike *microsoft* | where name -notlike *OneNote* | where name -notlike *fax* | select name,portname,drivername"')
printLogStatus = subprocess.getoutput('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command (Get-WinEvent -Listlog "Microsoft-Windows-PrintService/Operational).isEnabled"')
############################
###### Functions ##########
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def Welcome():
print("####################################################################################################")
prCyan("Run-PrintDiagnostics Version 1.1, Now in Python!\n Author: David Just")
prGreen(f"Welcome to the Printer Diagnostics Utility \n Where we try to make printers slightly less painful! \n Currently Running as {curUser.upper()}")
print("#################################################################################################### \r")
if activeUser != curUser:
prRed("Caution, you are currently running as curUser./nYou must run this tool as the logged on user in order to work with shared printers \nRunning as SYSTEM or another user will only show system wide printers!")
sleep(1)
def FetchPrintLogs():
if printLogStatus != 'True':
prGreen("System Log [Microsoft-Windows-PrintService/Operational] is disabled...Trying to enable now")
try :
shell.ShellExecuteEx(lpVerb = 'runas', lpFile = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', lpParameters="$log = Get-WinEvent -ListLog Microsoft-Windows-PrintService/Operational;$log.IsEnabled=$true;$log.SaveChanges()")
prGreen("Successfully Enabled Logging!\nPlease try to print a test page and check back for event logs.\n")
sleep(1)
except :
print("Failed to enable log")
sleep(1)
else:
os.system("powershell.exe -command \"Get-WinEvent -LogName Microsoft-Windows-PrintService/Operational | Out-GridView ; pause\"")
def RestartSpooler():
print("Restarting Print Spooler...")
shell.ShellExecuteEx(lpVerb = 'runas', lpFile = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', lpParameters="Get-Service Spooler | Stop-Service -force ; cmd /c \"del %systemroot%\System32\spool\printers* /Q\" ; Get-Service Spooler | Start-Service")
def PrintTestPage():
wmi_o = wmi.WMI('.')
wql = ("SELECT * FROM Win32_Printer")
wmiPrinters = wmi_o.query(wql)
i = 1
for a in wmiPrinters:
print(i, a.DeviceID)
i+=1
selection = int(input("Select a printer to send a test page to:"))
index = int(selection - 1)
selectedPrinter = str(wmiPrinters[index].DeviceID)
command = "Invoke-CimMethod -MethodName PrintTestPage -InputObject (Get-CimInstance win32_printer | where name -like " + '"' + selectedPrinter + '"' + ')'
encodedBytes = command.encode("utf-16LE")
encodedStr = base64.b64encode(encodedBytes)
subprocess.call("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -encodedcommand " + encodedStr.decode())
def OpenPrinterCP(Option):
match Option :
case 'Modern':
os.system("cmd.exe /c \"start ms-settings:printers\"")
case 'Classic':
os.system("cmd.exe /c \"start control printers\"")
def AddNewPrinter():
os.system("cmd.exe /c \"C:\\Windows\\System32\\rundll32.exe PRINTUI.DLL, PrintUIEntry /im\"")
def EndProgram():
print("Thank you for using the python Print Diagnostic Tool. Happy Printing!")
sleep(1)
exit(0)
def MainMenu():
if printLogStatus == 'True':
Status = 'Enabled'
else:
Status = 'Disabled'
prCyan(f"Printer Service Log Status: {Status}")
print ("Installed Printers:")
print (printers)
print("*Main Menu*")
prCyan("\rOptions: \n[1] Review Print Service Logs\n[2] Restart and Clear Print Spooler\n[3] Add a new printer\n[4] Open Classic Printer Control Panel\n[5] Open Modern Printer Settings Page\n[6] Print Test Page\n[7] Quit")
try :
MenuSelection = int(input("Please Enter A Selection:"))
match MenuSelection:
case 1:
FetchPrintLogs()
case 2:
RestartSpooler()
case 3:
AddNewPrinter()
case 4:
OpenPrinterCP("Classic")
case 5:
OpenPrinterCP("Modern")
case 6:
PrintTestPage()
case 7:
EndProgram()
except ValueError:
prRed("Please enter a valid selection")
os.system("pause")
MainMenu()
Welcome()
while "True":
MainMenu()