Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Set default printer $CheckBox with powershell

$
0
0

Hey I was hoping someone could help me out with this. I am building a GUI to add a printer and giving it a check box to set it to the default.  Everything is working correctly except that when it set it to the default it locks up the PowerShell ISE.  I have tried to change several thing to get it to work but I'm not having any luck.   Any help would be greatly appreciated.

Here is my script:

#Load Required visaual basic libraries for Input Windows
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null


#Static Variables
#---------------------------------------------------------------
#This is the path to the print server
$printserv= "................"
$x = 0 
$computername = Get-Content env:computername

#Object Properties
#---------------------------------------------------------------
#~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(199, 178)
$Form1.Text = "Form1"
#~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(13, 13)
$Label1.Size = New-Object System.Drawing.Size(172, 23)
$Label1.TabIndex = 5
$Label1.Text = "What is the name of your printer?"
#~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Point(12, 43)
$TextBox1.Size = New-Object System.Drawing.Size(173, 20)
$TextBox1.TabIndex = 3
$TextBox1.Text = ""
$TextBox1.CharacterCasing = 'Upper'
#~~< Button2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Point(110, 69)
$Button2.Size = New-Object System.Drawing.Size(75, 23)
$Button2.TabIndex = 2
$Button2.Text = "Cancel"
$Button2.UseVisualStyleBackColor = $true
#~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(12, 69)
$Button1.Size = New-Object System.Drawing.Size(75, 23)
$Button1.TabIndex = 1
$Button1.Text = "Add Printer"
$Button1.UseVisualStyleBackColor = $true
#~~< CheckBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$CheckBox1 = New-Object System.Windows.Forms.CheckBox
$CheckBox1.Location = New-Object System.Drawing.Point(12, 108)
$CheckBox1.Size = New-Object System.Drawing.Size(173, 24)
$CheckBox1.TabIndex = 0
$CheckBox1.Text = "Make the printer defualt?"
$CheckBox1.UseVisualStyleBackColor = $true
#~~< Defualt Printer >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Def = New-Object -ComObject WScript.network

$Form1.Controls.Add($Label1)
$Form1.Controls.Add($TextBox1)
$Form1.Controls.Add($Button2)
$Form1.Controls.Add($Button1)
$Form1.Controls.Add($CheckBox1)



#Button Functions
$Button1.Add_Click({
    if ($TextBox1.TextLength -gt 0){
        $printpath=$printserv+$TextBox1.text
        printui.exe /in /n $printpath
        Write-Host "Add a printer"
        }    
    If ($CheckBox1.Checked){
        #printui.exe /y /n $printpath | Out-Null
        $def.SetDefaultPrinter($printpath)
        Write-Host "Check Box If"
        }
    })

$Button2.Add_Click({$Form1.Close()})

$TextBox1.Select()

$Form1.ShowDialog()


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>