Ok so I am trying to get my feet wet in powershell. I want to start small so I figured that adding printer(s) using IP addresses would be the best way to do this. I found a script online that claims to work. I have made some changes to it to fit my environment.<\/p>\n
Here is what I’m using.<\/p>\n
Start-Process powershell.exe
\nSet-ExecutionPolicy -scope Process bypass
\n####################################################<\/p>\n
$PrinterIP = “1.1.1.1”
\n$PrinterPort = “TCP/IP”
\n$PrinterPortName = “IP_” + $PrinterIP
\n$DriverName = “SAVIN SP 8300DN PCL 6”
\n$DriverPath = \"\\unc_path\\to\\my\\drivers\"
\n$DriverInf = “\\unc_path\\to\\my\\drivers\\OEMSETUP.inf”
\n$PrinterCaption = “Printer_Name”
\n####################################################<\/p>\n
Function
\nCreatePrinterPort {
\nparam ($PrinterIP, $PrinterPort, $PrinterPortName)
\n$wmi = [wmiclass]“\\$ComputerName\\root\\cimv2:win32_tcpipPrinterPort”
\n$wmi.psbase.scope.options.enablePrivileges = $true
\n$Port = $wmi.createInstance()
\n$Port.name = $PrinterPortName
\n$Port.hostAddress = $PrinterIP
\n$Port.portNumber = $PrinterPort
\n$Port.SNMPEnabled = $false
\n$Port.Protocol = 1
\n$Port.put()
\n}
\nFunction InstallPrinterDriver {
\nParam ($DriverName, $DriverPath, $DriverInf)
\n$wmi = [wmiclass]“\\$ComputerName\\Root\\cimv2:Win32_PrinterDriver”
\n$wmi.psbase.scope.options.enablePrivileges = $true
\n$wmi.psbase.Scope.Options.Impersonation = `
\n[System.Management.ImpersonationLevel]::Impersonate
\n$Driver = $wmi.CreateInstance()
\n$Driver.Name = $DriverName
\n$Driver.DriverPath = $DriverPath
\n$Driver.InfName = $DriverInf
\n$wmi.AddPrinterDriver($Driver)
\n$wmi.Put()
\n}<\/p>\n
Function CreatePrinter { I am using Powershell ISE (running as as admin) when I run the first 2 lines of the script I do get the powershell window to come up and the prompt for \"The Execution policy helps you from scripts… \" I click yes and then I do the command PS C:\\WINDOWS\\system32> get-executionpolicy Where I’m failing is right after that when I try to run the whole script. The Script appears to run but it’s not creating the port or any part of it. I do not get any errors. I am using “Option 1” as I have several different printers in several differnt locations, and I don’t really need to set up a “mass” of PC’s right now. Ideally I would like to be able to add several printer to this script so I can just run it once and be done with it. Also not have to put in the name of the PC. I would like to just be logged in as admin (or myself … I am a domain admin) and run the script and all of the printers would be installed. Thank you.<\/p>","upvoteCount":7,"answerCount":10,"datePublished":"2018-04-11T13:28:33.000Z","author":{"@type":"Person","name":"jimhadley","url":"https://community.spiceworks.com/u/jimhadley"},"suggestedAnswer":[{"@type":"Answer","text":" Ok so I am trying to get my feet wet in powershell. I want to start small so I figured that adding printer(s) using IP addresses would be the best way to do this. I found a script online that claims to work. I have made some changes to it to fit my environment.<\/p>\n Here is what I’m using.<\/p>\n Start-Process powershell.exe $PrinterIP = “1.1.1.1” Function Function CreatePrinter { I am using Powershell ISE (running as as admin) when I run the first 2 lines of the script I do get the powershell window to come up and the prompt for \"The Execution policy helps you from scripts… \" I click yes and then I do the command PS C:\\WINDOWS\\system32> get-executionpolicy Where I’m failing is right after that when I try to run the whole script. The Script appears to run but it’s not creating the port or any part of it. I do not get any errors. I am using “Option 1” as I have several different printers in several differnt locations, and I don’t really need to set up a “mass” of PC’s right now. Ideally I would like to be able to add several printer to this script so I can just run it once and be done with it. Also not have to put in the name of the PC. I would like to just be logged in as admin (or myself … I am a domain admin) and run the script and all of the printers would be installed. Thank you.<\/p>","upvoteCount":7,"datePublished":"2018-04-11T13:28:33.000Z","url":"https://community.spiceworks.com/t/first-powershell-script/645613/1","author":{"@type":"Person","name":"jimhadley","url":"https://community.spiceworks.com/u/jimhadley"}},{"@type":"Answer","text":" Welcome!<\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>
\nparam ($PrinterCaption, $PrinterPortName, $DriverName)
\n$wmi = ([WMIClass]“\\$ComputerName\\Root\\cimv2:Win32_Printer”)
\n$Printer = $wmi.CreateInstance()
\n$Printer.Caption = $PrinterCaption
\n$Printer.DriverName = $DriverName
\n$Printer.PortName = $PrinterPortName
\n$Printer.DeviceID = $PrinterCaption
\n$Printer.Put()
\n}
\nforeach ($computer in $ComputerList) {
\nCreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort -PrinterPortName $PrinterPortName -ComputerName $computer InstallPrinterDriver -DriverName $DriverName -DriverPath<\/code>
\n$DriverPath -DriverInf $DriverInf -ComputerName $computer
\nCreatePrinter -PrinterPortName $PrinterPortName -DriverName `
\n$DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
\n}
\n####################################################<\/p>\n
\nBypass (and that’s showing)<\/p>\n
\nI have chanaged the IP address, path statements and PC name, but I have those correct in the script a<\/p>\n
\nSet-ExecutionPolicy -scope Process bypass
\n####################################################<\/p>\n<\/a>Change these values to the appropriate values in your environment<\/h1>\n
\n$PrinterPort = “TCP/IP”
\n$PrinterPortName = “IP_” + $PrinterIP
\n$DriverName = “SAVIN SP 8300DN PCL 6”
\n$DriverPath = \"\\unc_path\\to\\my\\drivers\"
\n$DriverInf = “\\unc_path\\to\\my\\drivers\\OEMSETUP.inf”
\n$PrinterCaption = “Printer_Name”
\n####################################################<\/p>\n<\/a>ComputerList Option 1<\/h3>\n
<\/a>$ComputerList = @(“Xname of my PC”)<\/h1>\n
<\/a>ComputerList Option 2<\/h3>\n
<\/a>$ComputerList = @()<\/h1>\n
<\/a>Import-Csv “C:\\Temp\\ComputersThatNeedPrinters.csv” | `<\/h1>\n
<\/a>% {$ComputerList += $_.Computer}<\/h1>\n
\nCreatePrinterPort {
\nparam ($PrinterIP, $PrinterPort, $PrinterPortName)
\n$wmi = [wmiclass]“\\$ComputerName\\root\\cimv2:win32_tcpipPrinterPort”
\n$wmi.psbase.scope.options.enablePrivileges = $true
\n$Port = $wmi.createInstance()
\n$Port.name = $PrinterPortName
\n$Port.hostAddress = $PrinterIP
\n$Port.portNumber = $PrinterPort
\n$Port.SNMPEnabled = $false
\n$Port.Protocol = 1
\n$Port.put()
\n}
\nFunction InstallPrinterDriver {
\nParam ($DriverName, $DriverPath, $DriverInf)
\n$wmi = [wmiclass]“\\$ComputerName\\Root\\cimv2:Win32_PrinterDriver”
\n$wmi.psbase.scope.options.enablePrivileges = $true
\n$wmi.psbase.Scope.Options.Impersonation = `
\n[System.Management.ImpersonationLevel]::Impersonate
\n$Driver = $wmi.CreateInstance()
\n$Driver.Name = $DriverName
\n$Driver.DriverPath = $DriverPath
\n$Driver.InfName = $DriverInf
\n$wmi.AddPrinterDriver($Driver)
\n$wmi.Put()
\n}<\/p>\n
\nparam ($PrinterCaption, $PrinterPortName, $DriverName)
\n$wmi = ([WMIClass]“\\$ComputerName\\Root\\cimv2:Win32_Printer”)
\n$Printer = $wmi.CreateInstance()
\n$Printer.Caption = $PrinterCaption
\n$Printer.DriverName = $DriverName
\n$Printer.PortName = $PrinterPortName
\n$Printer.DeviceID = $PrinterCaption
\n$Printer.Put()
\n}
\nforeach ($computer in $ComputerList) {
\nCreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort -PrinterPortName $PrinterPortName -ComputerName $computer InstallPrinterDriver -DriverName $DriverName -DriverPath<\/code>
\n$DriverPath -DriverInf $DriverInf -ComputerName $computer
\nCreatePrinter -PrinterPortName $PrinterPortName -DriverName `
\n$DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
\n}
\n####################################################<\/p>\n
\nBypass (and that’s showing)<\/p>\n
\nI have chanaged the IP address, path statements and PC name, but I have those correct in the script a<\/p>\n