ps script to install printers

Hi,

I’m trying to learn powershell scripting since it ease Systems Admin’s tasks for automation, so I’d like to set a small lab where the purpose of this script is to deploy printers once a user login. The script below is part of my research and correct/educate me if I’m doing it wrong.

#Set runpath to local drive
C:

Adding of printer

Add-Printer -ConnectionName “\<print server>&lt;printer name1>”
Add-Printer -ConnectionName “\<print server>&lt;printer name2>”

#Get the infos of all printer

$Printers = Get-WmiObject -Class Win32_Printer

If($PSCmdlet.ShouldProcess(“$PrinterName”,“Set the default printer”))
{
Try
{
Write-Verbose “Get the specified printer info.”
$Printer = $Printers | Where{$_.Name -eq “$PrinterName”}

If($Printer)
{
Write-Verbose “Setting the default printer.”
$Printer.SetDefaultPrinter() | Out-Null

Write-Host “Successfully set the default printer.”
}
Else
{
Write-Warning “Cannot find the specified printer.”
}
}
Catch
{
$ErrorMsg = $_.Exception.Message
Write-Host $ErrorMsg -BackgroundColor Red
}
}

Hello Tech,

Welcome to the forums! We are happy you came here! Quick question, your script is calling out $PrinterName variable and I’m not seeing it anywhere in your script? Is this the entire script? Please be sure to include the entire code minus any proprietary/confidential information.

Yes this is the full script which I made from research. I think I need to spcify a variable which coincides with $PrinterName?

Yes, that is correct. What version of the Windows Operating System are your users using? If its Windows 10 the last printer used is set as the default printer normally.

Well for some users they’re on Win 10 but for the bulk users, they’re remotely connecting on an RDS server and RDS is Server 2016.

 

Tech,

My first recommendation is to comment some of your code with what you are trying to do. This will help me to follow along with the logic you are using. I could look at writing something similar, but that won’t help you learn. For RDS 2016 you aren’t trying to set up any default printer for applications, are you? Some applications are known to have issues with RDS 2016. The forum post below is one example.

https://social.technet.microsoft.com/Forums/office/en-US/0cb51b0d-c1f2-4b70-93db-b1c7aa423d30/server-2016-rds-default-printer-not-working?forum=winserverTS

Hmmm…Unfortunately, they do have a program developed from Delphi and before it will work properly, it checks for a default printer, if there’s none, it will error out and not open. So their process is before opening the program, they check if there’s a default printer set by the server before they open the program.

I appreciate your help on the code. I can test this in my lab environment and see how it goes.

Thanks

TECH

Hold on, the link you sent was for Server 2016, the one I’m working on is 2012 R2

Hey Tech,

[quote quote=173257]Well for some users they’re on Win 10 but for the bulk users, they’re remotely connecting on an RDS server and RDS is Server 2016.
[/quote]

You did state RDS Server 2016. I would still go ahead and test out the code.

yeah my typo error on the 2016. Ok, I’ll test the code and see how it goes.