[英]PowerShell Script to Change Printer Drivers on Several Printers
I'm trying to make a PowerShell script that will change all the drivers for a specific set of printers. 我正在尝试制作一个PowerShell脚本,该脚本将更改一组特定打印机的所有驱动程序。
I have about 200 printers whose name begins with the letter Z. I also have a handful of printers that don't begin with the letter Z. 我大约有200台打印机的名称以字母Z开头。我也有一些不以字母Z开头的打印机。
What i'm trying to accomplish is this... Any printer beginning with the letters ZEB has their driver changed to "HP LaserJet 4000 Series PS" 我要实现的目标是...任何以字母ZEB开头的打印机,其驱动程序都已更改为“ HP LaserJet 4000 Series PS”
I've tried modifying the script below to work with what i need, but it just runs and nothing changes. 我尝试修改下面的脚本来满足我的需要,但是它可以运行并且没有任何变化。
$driver = "HP LaserJet 4000 Series PS"
$pattern = 'ZEB'
$printers = gwmi win32_printer
foreach($printer in $printers){
$name = $printer.name
if($name -like $pattern){
& rundll32 printui.dll PrintUIEntry /Xs /n $name DriverName $driver
}
}
This is fairly simple, as you already have half the stuff done from the comment response. 这非常简单,因为您已经完成了评论响应中的一半工作。 I'm going to filter the printers that you want to modify as the loop is defined, so you only put the printers you want through the loop and the rest are skipped entirely.
我将在定义循环时过滤要修改的打印机,因此只将所需的打印机放入循环中,其余打印机将被完全跳过。 The main thing is the
Where
statement, which is working like your If
statement to filter out just the right printers. 最主要的是
Where
语句,它的工作方式与If
语句一样,可以过滤出正确的打印机。 It reads like this: 内容如下:
$Printers | Where{ $_.Name -like $pattern -and $_.DriverName -like '*HP LASERJET 4*' }
So it checks that the name starts with the letters ZEB, and checks that the drivers have 'HP LASERJET 4' somewhere in the driver name. 因此,它会检查名称是否以字母ZEB开头,并检查驱动程序名称中是否有“ HP LASERJET 4”。 All together it looks like this:
在一起看起来像这样:
$driver = "HP LaserJet 4000 Series PS"
$pattern = 'ZEB*'
$printers = gwmi win32_printer
foreach($printer in ($printers|Where{$_.Name -like $pattern -and $_.DriverName -like '*HP LASERJET 4*'})){
$name = $printer.name
& rundll32 printui.dll PrintUIEntry /Xs /n $name DriverName $driver
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.