简体   繁体   English

我需要使用 Powershell 和单个 .csv 文件将多台打印机添加到多个打印服务器

[英]I need to add multiple printers to multiple print servers with Powershell and a single .csv file

I have a script that will run against an Excel file for a list of printers as well as the the listed print server.我有一个脚本,它将针对打印机列表以及列出的打印服务器的 Excel 文件运行。

The issue I cant figure out is how to add multiple printers to multiple print servers without having an Excel sheet with 200 lines.我无法弄清楚的问题是如何在没有 200 行的 Excel 工作表的情况下将多台打印机添加到多个打印服务器。

I would like to have the print servers listed in the Powershell script and have the foreach command go through each server listed dynamically.我希望在 Powershell 脚本中列出打印服务器,并让foreach命令通过动态列出的每个服务器。

Here is the code I have so far.这是我到目前为止的代码。 I am trying to get the $server to be able to have a list of servers and change the part in the function to the next server on the list.我试图让$server能够拥有服务器列表并将函数中的部分更改为列表中的下一个服务器。 I attached an example of what the .csv formatting looks like.我附上了一个 .csv 格式的示例。 example of the formatting of the .csv file Thanks for any help anyone can provide. .csv 文件格式示例感谢任何人提供的任何帮助。

function CreatePrinter {
    $server = $args[0]
    $print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance() 
    $print.drivername = $args[1]
    $print.PortName = $args[2]
    $print.Shared = $false
    $print.Sharename = $args[3]
    $print.Location = $args[4]
    $print.Comment = $args[5]
    $print.DeviceID = $args[6]
    $print.Put() 
}

function CreatePrinterPort {
    $server =  $args[0] 
    $port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
    $port.Name= $args[1]
    $port.SNMPEnabled=$false 
    $port.Protocol=1 
    $port.HostAddress= $args[2]
    $port.Put() 
}

$printers = Import-Csv C:\test\NewPrinters.csv

foreach ($printer in $printers) {
    CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
    CreatePrinter $printer.Printserver $printer.Driver $printer.Portname 
    $printer.Sharename $printer.Location $printer.Comment $printer.Printername
}

No need to reinvent the wheel when you can use the existing commands for printer management.当您可以使用现有命令进行打印机管理时,无需重新发明轮子。

Add-PrinterPort and Add-Printer will what you want: Add-PrinterPortAdd-Printer将满足您的需求:

foreach ($printer in $printers) {
    Add-PrinterPort -ComputerName $printer.Printserver -Name $printer.Portname -PrinterHostAddress $printer.IPAddress
    Add-Printer -ComputerName $printer.Printserver -Name $printer.Printername -DriverName $printer.Driver -Shared -ShareName $printer.Sharename -PortName $printer.Portname -Comment $printer.Comment -Location $printer.Location
}

EDIT:编辑:

For multiple servers:对于多台服务器:

$servers = "printserver01","printserver02","printserver03"

foreach ($server in $servers) {
    foreach ($printer in $printers) {
        Add-PrinterPort -ComputerName $server -Name $printer.Portname -PrinterHostAddress $printer.IPAddress
        Add-Printer -ComputerName $server -Name $printer.Printername -DriverName $printer.Driver -Shared -ShareName $printer.Sharename -PortName $printer.Portname -Comment $printer.Comment -Location $printer.Location
    }
}

Also no need to say Sorry... You took the time out of your day to answer a question for some random dude.也不需要说对不起......你抽出时间来回答一些随机的家伙的问题。 This is what my final script looks like and it works flawlessly on Windows Server 2012.这就是我的最终脚本的样子,它可以在 Windows Server 2012 上完美运行。

foreach ($server in @("Printserver1","Printserver2","Printserver3")) {
   foreach ($printer in @(Import-Csv C:\PrinterList.csv)) {
      Add-PrinterPort -ComputerName $server -Name $printer.IPAddress -PrinterHostAddress $printer.IPAddress
      Add-Printer -ComputerName $server -Name $printer.Printername -DriverName $printer.Driver -PortName $printer.IPAddress -Comment $printer.Comment -Location $printer.Location
   }
}

-Nick- -尼克-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM