PowerShell - Project Final


PowerShell Project
The print spooler is a service application that manages printer jobs sent from a computer to printer or print server, allowing for a buffer - though more commonly known as a queue. The print queue as described, is a queue of stored printing tasks that execute in chronological order, allowing for fair use and management of printer resources. While there are various reasons a host may receive errors pertaining to print spooler communication, I decided to write a PowerShell script to run a series of commonly used administrative tasks that simply restarts the service as quick fix script, to meet slightly exceed the expectations of the project as it is a common adminstrative task.
To perform this task using the Windows GUI, an Administrator could:
Windows Key + R and type services.msc
Once ran will bring up the Services dialog as seen below. At this point we look for “Print Spooler”, right click where we can then stop start or restart the service.

The Script
# The purpose of this script is restart the print spooler after detecting whether or not it is running.
$ServiceName = 'spooler'
$arrService = Get-Service -Name $ServiceName
write-host "
            _       _            
           (_)     | |          
 _ __  _ __ _ _ __ | |_
| '_ \| '__| | '_ \| __|
| |_) | |  | | | | | |_  
| .__/|_|  |_|_| |_|\__|
| |     Spool3r KiLLa 5000 v2-_                    
|_|   _- jimdotdev@gmail.com -_
"
start-sleep -s 2 # time delays
write-host ""
write-host "Checking current spooler status"
start-sleep -s 2
write-host ""
if ($arrService.Status -ne 'Running') # checks the current spooler status and see if it is not equivalent to "Running" if it is, it attempts to start it
{                                     # if it is not, it moves on to elseif
    write-host ""
    write-host "It Looks like the printer spooler is not running"
    write-host ""
    start-sleep -s 2
    write-host "Attempting to start Print Spooler"
    write-host ""
    Start-Service $ServiceName
    Start-Sleep -seconds 5
    $arrService.Refresh()
    if ($arrService.Status -eq 'Running')
    {
        Write-Host " The Print Spooler is now Running"
    }
   
}
elseif ($arrService.Status -eq 'Running') #Checks to see if the printer is equivlant to "running" and if is, stops and restarts it to restart the spooler
    {
    write-host " The Print Spooler is already running"
    start-sleep -s 2
    write-host ""
    write-host "Attempting to stop the Print Spooler..."
    write-host ""
    Stop-Service $ServiceName -force
    start-sleep -s 2
    write-host " Now attempting to restart the Print Spooler..."
    Start-service $ServiceName
    start-sleep -s 2
    $arrService.Refresh() #refreshes the request to recheck status
    if ($arrService.Status -eq 'Running')#Makes sure the print spool is in fact running again
    {
        write-host ""
        Write-Host "The Print Spooler has been restarted and is currently running"
        Start-sleep -s 2
        }
        }
    if ($arrService.Status -eq 'Running') # if the print spooler is running, should we print a test page?
    {
    write-host ""
        $answer = Read-Host "Should we attempt to print a test page? Yes/No" #Read-host waits for response from host then uses if statements based on option
        if ($answer -eq "yes" -or $answer -eq "y")
        {
        write-host ""
        "This is a test page generated by the Print Spool3r KiLLa 5000 v2 written by jimdotdev. Jim is a Network Administrator & Ethical Hacker providing diversity through a wide
        arrange of skillsets. To learn more, you can visit his blog  @                    
  (_|_)             | |     | |    | |                    
   _ _ _ __ ___   __| | ___ | |_ __| | _____   _____ __ _
  | | | '_ ` _ \ / _` |/ _ \| __/ _` |/ _ \ \ / / __/ _` |
  | | | | | | | | (_| | (_) | || (_| |  __/\ V / (_| (_| |    
  | |_|_| |_| |_|\__,_|\___/ \__\__,_|\___| \_(_)___\__,_|
  / |                                                    
|__/    jimdotdev.ca | jimdotdev@gmail.com
" | Out-Printer # this is the text being piped to the out-printer which will be what is seen as a test page
        write-host "A test page has been attempted"
         
    }
    if ($answer -eq "no" -or $answer -eq "n")
    {
    start-sleep -s 2
    write-host ""
    write-host "No test page will be printed and the script will now self terminate"
    exit
    }
    }

General Process Flow Of Script
For my current administrative role, this script is convenient as it requires little interaction with the host. Once the script is ran, the service will restart which normally fixes print spooler issues.
Expanding further:
In version 1 of this script, I was backing up the printer spooler folder to another location on the drive. I removed this function as I believed it to be unnecessary as I don't feel the spooler documentation to be critical data. To expand on it more, I would re-add this feature as an unnecessary precaution.

 ---------------------------------------------------------------------------------------------------------------------------------

.

Comments