简体   繁体   English

从导入的 csv 文件创建打印机到多个打印服务器

[英]Create printers from an imported csv file to multiple print servers

I have been working with this script and have successfully grabbed info from a .csv file and added it to one print server.我一直在使用这个脚本并成功地从 .csv 文件中获取信息并将其添加到一台打印服务器。

Right now I have the print server hard coded in the script and it allows me to add multiple print servers into the script, but I would like to add the print servers to a column in my .csv file and read from there to eliminate the static servers in the code.现在我在脚本中硬编码了打印服务器,它允许我将多个打印服务器添加到脚本中,但我想将打印服务器添加到我的 .csv 文件中的一列并从那里读取以消除静态代码中的服务器。 Here is what I have:这是我所拥有的:

The second part I am struggling with is publishing and not publishing printers ( listing in AD or not ) I was thinking of adding another column called published.我正在努力解决的第二部分是发布而不是发布打印机(是否在 AD 中列出)我正在考虑添加另一个名为已发布的列。 Then creating an if/then to publish or not publish**然后创建一个 if/then 来发布或不发布**

foreach ($server in @("printserver1")) {
    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 -Shared -ShareName $printer.Printername -Published
    }
}

If PrinterList.csv contains a column called Publish with False or True as possible values, you can do the following:如果PrinterList.csv包含一个名为Publish的列,其中可能的值是FalseTrue ,您可以执行以下操作:

foreach ($printer in (Import-Csv C:\PrinterList.csv)) {
    $Params = @{ ComputerName = $server
                 Name = $printer.Printername
                 DriverName = $printer.Driver
                 PortName = $printer.IPAddress
                 Comment = $printer.Comment
                 Location = $printer.Location
                 ShareName = $printer.Printername
    }
    Add-Printer @Params -Shared -Published:([bool]::Parse($printer.Publish))
}

Since Publish is a [switch] parameter, you can use the syntax -Publish:$true or -Publish:$false .由于Publish[switch]参数,您可以使用语法-Publish:$true-Publish:$false The Parse() method parses a string value into a boolean value. Parse()方法将字符串值解析为布尔值。

$Params Splatting is not necessary here. $Params在这里不需要Splatting It just provides a bit more readability.它只是提供了更多的可读性。

Alternatively, [System.Convert]::ToBoolean($printer.Publish) has the same result in the proposed scenario but does offer more flexibility as [System.Convert]::ToBoolean(0) returns False and [System.Convert]::ToBoolean(1) returns True .或者, [System.Convert]::ToBoolean($printer.Publish)在建议的场景中具有相同的结果,但确实提供了更大的灵活性,因为[System.Convert]::ToBoolean(0)返回False[System.Convert]::ToBoolean(1)返回True

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

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