How to use a Function in Powershell

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Richard Diphoorn Richard Diphoorn 8 months ago.

  • Author
    Posts
  • #37514
    Profile photo of wchristner
    wchristner
    Participant

    I have a script I am using from Kris Powell. To install printers. From what I can see I need to call these functions before it will actually install a printer via IP Address. any ideas on how i do this?
    Here is what I have

    $PrinterIP = “192.168.141.20”
    $PrinterPort = “9100”
    $PrinterPortName = “IP_192.168.141.20” + $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 = $9100
    $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_
    }
    

    Output is as follows

    PS C:\windows\system32> C:\Users\christnerw\Desktop\printer test.ps1
    
    PS C:\windows\system32> 

    So basically I am not getting any errors (Score)

    Thanks

  • #37520
    Profile photo of Nick Hawkins
    Nick Hawkins
    Participant

    So executing that script is going to define those functions in a new scope and then it will immediately go away once the script is finished running. To actually be able to execute the functions after this script runs you could use the "dot-sourcing" method which would define these functions in your current PowerShell scope, at that point you could actually run the functions.

    Rather than having me explain this check out this post by Ed Wilson where he explains this:

  • #37521
    Profile photo of wchristner
    wchristner
    Participant

    Thanks for the info I will check out the link

  • #37524
    Profile photo of Justin King
    Justin King
    Participant

    Another way to go is you can actually save that file as a .psm1 then simple import-module file.psm1 and that will load the functions into the current context if you wish to run it in your current console. Nice easy way to test your functions to make sure they behave as you expect. In WMF5, you'll get a warning as the functions don't follow the proper format but they will work (CreatePrinterPort should be New-PrinterPort, for example). You can use Get-verb for the list of "approved verbs", but you can ignore this list if the function will only exist within other scripts (not meant for console consumption)

    If you ultimately want this in a login script or something else, follow Nick's link, or place the final script at the end of the code you started (if you don't need these functions to exist outside the final script).

  • #37532
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    wchristner: I don't get completely why you post this question. I thought it was already answered with this: https://powershell.org/forums/topic/function-createprinter/#post-37455

    You are basically asking again the same question.

You must be logged in to reply to this topic.