ps script to install printers

Welcome Forums General PowerShell Q&A ps script to install printers

This topic contains 9 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #173041

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

    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 "\\\"
    Add-Printer -ConnectionName "\\\"

    # #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
    }
    }

  • #173053

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    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.

  • #173218

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

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

  • #173254

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    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.

  • #173257

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

    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.

     

  • #173275

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    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

  • #173278

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

    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

  • #173419

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

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

  • #173434

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Hey Tech,

    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.

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

  • #173446

    Participant
    Topics: 2
    Replies: 5
    Points: 34
    Rank: Member

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

You must be logged in to reply to this topic.