4
\$\begingroup\$

I am attempting to clean up my code as best I can, but I am new to powershell and would like other's opinions on if there is anything else I can clean my code up with and any tips to overall improve writing my code in the future. This code is meant to:

  1. Disable a registry key in the background so that users can control their default printer
  2. Add a printer by simply typing in the network printer's name
  3. Let the user choose if they want to set the printer as their default
  4. Throw an error if there is no printer on the server that matches the inputted printer
#Sets the powershell console's size - set to minimum size so that the GUI is noticeable but the powershell console is not
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.windowsize
$newsize.height = 1
$newsize.width = 1
$pswindow.windowsize = $newsize

$key = Get-ItemPropertyValue -path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode"  #Grabs value for the "Legacy Default Printer Mode" registry key, which disables "Let windows manage my default printer" if the key is enabled and stores in $key variable
if ($key -eq 0) { Set-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value "1" } #Takes $key variable and sees if it is set to 0, if $key is set to 0 the string will set the key's value to 1 (this enables "Legacy Default Printer Mode" and disables "Let windows manage my default printer"

Add-Type -AssemblyName System.Windows.Forms #Adds the "Forms" assembly - creates and ties together forms, buttons, labels, etc.
Add-Type -AssemblyName System.Drawing #Adds the "Drawing" assembly - sets dialog location and size

function dialog { #Function used to create each dialog box
    param ( #Input parameters
        [string]$dialogFormTitle, #Dialog's title
        [string]$acceptButtonText, #Accept button's text
        [int]$L, #X position of cancel button
        [int]$R, #Y position of cancel button
        [string]$cancelButtonText, #Cancel button's text
        [string]$labelText, #Dialog's text
        [string]$inputCheck #Checks to see if you want a textbox (input "y" if you do)
    )

    #Drawing of dialog's box size, title, and position
    $form = New-Object System.Windows.Forms.Form
    $form.Text = $dialogFormTitle
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

    if ($acceptButtonText -ne "n") { #Checks to see if you want an accept button
        #Drawing of accept button's size, text, position, and setting of workflow for the dialog - if pressed this attempts to add the printer
        $acceptButton = New-Object System.Windows.Forms.Button
        $acceptButton.Location = New-Object System.Drawing.Point(75,120)
        $acceptButton.Size = New-Object System.Drawing.Size(75,23)
        $acceptButton.Text = $acceptButtonText
        $acceptButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form.AcceptButton = $acceptButton
        $form.Controls.Add($acceptButton) #Adds accept button to dialog
    }

    #Drawing of cancel button's size, text, position, and setting of workflow for the dialog - if pressed this ends the script
    $cancelButton = New-Object System.Windows.Forms.Button
    $cancelButton.Location = New-Object System.Drawing.Point($L,$R)
    $cancelButton.Size = New-Object System.Drawing.Size(75,23) 
    $cancelButton.Text = $cancelButtonText
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::No
    $form.CancelButton = $cancelButton
    $form.Controls.Add($cancelButton) #Adds cancel button to dialog

    #Drawing of the Read-Only text's size, text, and position for the dialog
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,50)
    $label.Text = $labelText
    $form.Controls.Add($label) #Adds RO text to dialog

    if ($inputCheck -eq "y") { #Checks to see if you want a text box
        #Drawing of the text input field's size and position for the dialog
        $textBox = New-Object System.Windows.Forms.TextBox
        $textBox.Location = New-Object System.Drawing.Point(10,70)
        $textBox.Size = New-Object System.Drawing.Size(260,20)
        $form.Controls.Add($textBox) #Adds input field to dialog
    }

    $form.Topmost = $true #Forces the dialog to the top of all programs when it first opens

    if ($inputCheck -eq "y") { $form.Add_Shown({$textBox.Select()}) } #Displays the dialog and it's child properties and checks to see if you wanted a text box
    $result = $form.ShowDialog()

    Set-Variable -Name "textBoxHolder" -value ($textBox.Text) -scope global #Creates a global variable named "textBoxHolder" and sets it to the value stored in $textBox.Text
    Set-Variable -Name "resultHolder" -value ($result) -scope global #Creates a global variable named "resultHolder" and sets it to the value stored in $result
}

dialog "Install Printer" "Ok" 150 120 "Cancel" "Enter printer name below:" "y" #Printer install dialog asking for printer's name

if ($resultHolder -eq [System.Windows.Forms.DialogResult]::OK) { #Code that adds the printer if "OK" is selected
    try {
        $x = '\\PRINTSERVER\'+$textBoxHolder #Takes the user's input and combines it with \\PRINTSERVER\ and stores it into the $x variable
        add-printer -ConnectionName $x -ErrorAction Stop #Adds the printer - if there is an error it catches the error
    }
    catch {
        dialog "Error" "n" 112.5 120 "Exit" "Error: The typed printer did not match a printer on the server. If you believe that you typed the printer correctly and need further assistance please contact the help desk at 319-861-7777." #Error dialog letting the user know that the printer they typed did not match a printer on the sserver. It suggests that the user try again and make sure they type it in correctly and to call the help desk if further assistance is needed
        
        Exit #Exits the script
    }
    dialog "Default Printer" "Yes" 150 120 "No" "Do you want this to be your default printer?" #Default printer dialog asking the user if they want to set the previously installed printer as their default

    if ($resultHolder -eq [System.Windows.Forms.DialogResult]::OK) { (New-Object -ComObject WScript.Network).SetDefaultPrinter($x) } #Code that sets printer to default if user selects "Yes"
}
\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.