Thursday, November 6, 2008

Creating Printer Install Batch Files

Was given the task to write a PowerShell script that would do the following:

  • Given a supplied list of host name, find the hostnames that have a specific named printer. In this case "osu_mc" will be in the Name property.
  • If a printer is found, create a batch file that is titled from the supplied host name.
  • The batch file should contain these 2 lines for every printer
    "rundll32 printui.dll,PrintUIEntry /in /q /n" and
    the printer name:
    (i.e. file:////OSU/NDPS_HP4350.OSU.HOSP.CAMPUS.OSU)

Here is the script that meets the need!

Function Get-PrinterName ([string]$HostName) {
Begin {}
Process {
-Host "HostName: "$_
= "C:\PrinterBatch\" + $_ + ".bat"
$file = New-Item -type file $FileName

= Get-WmiObject `
-Class Win32_Printer`
-ComputerName $_ `
-ErrorAction SilentlyContinue

ForEach ($Printer
in $Printers){ if ($Printer.Name -match 'osu_mc'){
-Content -Path $FileName `
-value "rundll32 printui.dll,PrintUIEntry /in /q /n"
-Content -Path $FileName `
-value $Printer.Name

-Content 'C:\HostNames.txt' Get-PrinterName