Function CreatePrinter

This topic contains 10 replies, has 2 voices, and was last updated by Profile photo of wchristner wchristner 5 months, 2 weeks ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #37362
    Profile photo of wchristner
    wchristner
    Participant

    I am a noob at PowerShell and I am looking for help finishing this script. I am stuck on the Create Printer Function. I want to install a printer via TCP IP. No print server in our environment. Later I want to add some choice menus, This would allow users on our network to come in from different offices, choose locations and choose printer closet to them, but for now getting this test installed would be great. Here is what I got so far

    $PrinterIP = “192.168.141.20”
    $PrinterPort = “9100”
    $PrinterPortName = “IP_” + $PrinterIP
    $DriverName = “Ricoh Aficio MP C6000 PS”
    $DriverPath = “\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA”
    $DriverInf = “\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA\oemsetup.inf”
    $PrinterCaption = “BH DockSide Color”
    
    Function CreatePrinterPort {
    param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
    $wmi = [wmiclass]”\\$ComputerName\root\cimv2:win32_tcpipPrinterPort”
    $wmi.psbase.scope.options.enablePrivileges = $true
    $Port = $wmi.createInstance()
    $Port.name = $192.168.141.20
    $Port.hostAddress = $192.168.141.20
    $Port.portNumber = $1900
    $Port.SNMPEnabled = $false
    $Port.Protocol = 1
    $Port.put()
    }
    
    Function InstallPrinterDriver {
    Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
    $wmi = [wmiclass]”\\$ComputerName\Root\cimv2:Win32_PrinterDriver”
    $wmi.psbase.scope.options.enablePrivileges = $true
    $wmi.psbase.Scope.Options.Impersonation = `
    [System.Management.ImpersonationLevel]::Impersonate
    $Driver = $wmi.CreateInstance()
    $Driver.Name = $"Ricoh Aficio MP C6000 PS"
    $Driver.DriverPath = $\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA
    $Driver.InfName = $\\bh-miworks-srv2\PrintDrivers\Ricoh MP c6501 e7200 Dockside Color\Win 10 PS\English\Prntdrvr\Ps_drvr\Win_2K_XP_VISTA\oemsetup.inf
    $wmi.AddPrinterDriver($Driver)
    $wmi.Put()
    }
    
    Function CreatePrinter {
    param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
    $wmi = ([WMIClass]”\\$ComputerName\Root\cimv2:Win32_Printer”)
    $Printer = $wmi.CreateInstance()
    $Printer.Caption = $"BH DockSide Color"
    $Printer.DriverName = $"Ricoh Aficio MP C6000 PS"
    $Printer.PortName = $192.168.141.20
    $Printer.DeviceID = $"DockSide Color"
    $Printer.Put()
    

    any help or suggestions would be appreciated.

    PS sorry if my code is not properly formatted. if someone has a document or example I will be sure to follow next time.
    Thanks

    #37370
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    I see this is the same code as posted by Kris Powell. http://www.adminarsenal.com/admin-arsenal-blog/how-to-add-printers-with-powershell/

    That script is not working for you?

    #37373
    Profile photo of wchristner
    wchristner
    Participant

    It is, I changes the variables to match our environment and printers, so far the error I get is this:

    At C:\Users\christnerw\Desktop\printer test.ps1:36 char:24
    + Function CreatePrinter {
    +                        ~
    Missing closing '}' in statement block.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndCurlyBrace
    

    If i am missing a "}" I haven't found it yet.

    thanks

    #37375
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Put the closing curly brace after $Printer.Put()

    #37376
    Profile photo of wchristner
    wchristner
    Participant

    Cool no Error, But no Printer installed either.

    I am closer now. I can't believe I didn't see that. Wow I am a Noob
    Thanks

    #37377
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Which OS are you using? From Windows 8 and higher you have add-printer available. 🙂

    #37421
    Profile photo of wchristner
    wchristner
    Participant

    Windows 7 SP1
    The orgional script calls for the

    ### ComputerList Option 1 ###
    # $ComputerList = @(“lana”, “lisaburger”)
    
    ### ComputerList Option 2 ###
    # $ComputerList = @()
    # Import-Csv “C:\Temp\ComputersThatNeedPrinters.csv” | `
    # % {$ComputerList += $_.Computer}

    and the

     foreach ($computer in $ComputerList) {
    CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort `
    -PrinterPortName $PrinterPortName -ComputerName $computer
    InstallPrinterDriver -DriverName $DriverName -DriverPath `
    $DriverPath -DriverInf $DriverInf -ComputerName $computer
    CreatePrinter -PrinterPortName $PrinterPortName -DriverName `
    $DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
    }

    I wonder if I need to do this as well. though I don't see how the Printer is not installing . Oh well back to the drawing board!

    #37422
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Yeah, you need to "do" something with the functions. The functions declare only code; they don't do anything until you load them into memory, and execute them. That is what the code is doing what you showed.

    #37448
    Profile photo of wchristner
    wchristner
    Participant

    Ok I will look into "do" I appreciate your insights. I have learned a lot so far. I you can point me in the right direction that would be awesome. however google is my friend.

    thanks again

    #37455
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    You were saying that you are new to PowerShell. I would recommend you to read these two books:
    https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-second-edition
    https://www.manning.com/books/learn-powershell-toolmaking-in-a-month-of-lunches

    In these books you will learn what functions are and how you use them. I could do a whole story about functions and how they have to be used, but I think I'm better off to give you two good resources.

    #37500
    Profile photo of wchristner
    wchristner
    Participant

    Thanks I will definitely check out these books today!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.