powershell - creating dynamic printer script - output and command stucture
I am attempting to create a script using powershell that I can use to make a list of:
- network printers a user currently has installed
- printer drivers that are currently installed on the users PC
I have managed to come up with the following script. I am still very new to powershell and I know it isn't the prettiest, but it is working to some degree.
$username=[Environment]::UserName
$machinename=[Environment]::MachineName
## Get installed printers and create template command
$printers=get-WmiObject Win32_printer -Property Name | where { $_.Name -like "\\printserver1\*" -or $_.Name -like "\\printserver2\*" }
$printers2=$printers | select-object Name
$printers2 > C:\testdir\"$username"PRINTERS.txt
$printers3=get-content C:\testdir\"$username"PRINTERS.txt | select-string -pattern "\\"
foreach ( $y in $printers3 )
{
write-host RUNDLL32 PRINTUI.DLL','PrintUIEntry /dn /n'"'$y
}
## Get drivers and create template command
$printerdrivers=get-wmiobject -class Win32_PrinterDriver -Property Name
$printerdrivers2=$printerdrivers -split('=') #| select-string 'Name'
$printerdrivers3=$printerdrivers2 | select-string 'HP'
foreach ( $i in $printerdrivers3 )
{
$removeprinter=$i -split (',') | select-string 'HP'
#write-host $removeprinter
write-host rundll32 printui.dll','PrintUIEntry /dd /c\\$machinename /m $removeprinter'"' /h "x64" /v "Type 3 - User Mode"
}
the above code produces the following output:
RUNDLL32 PRINTUI.DLL,PrintUIEntry /dn /n"\\printserver1\ACC_PRINT_HP9040UPS
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.1)" /h x64 /v Type 3 - User Mode
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.2)" /h x64 /v Type 3 - User Mode
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Designjet T770 24in HPGL2" /h x64 /v Type 3 - User Mode
where I am having problems is in two places.
Firstly, is it possible for me to run these commands from within the same script without outputting them to a text file and running them as a batch file? When I attempting to run the template commands I keep getting an error saying that the printer doesn't exist. Unfortunately I can't tell how powershell is actually formulating the command.
Secondly, for the printers installed section, I can't get quotation marks around the printer name for some reason when I do the write-host command. I can get it to show up at the beginning of the printer name ""\printerserver1\ACC_PRINT_HP9040UPS" , but not at the end.
I know this code is probably really bad, but I am hoping that I have something here that I can work with. My primary goal was to have something dynamic that I can run against any computer and have it clean up the printers and printer drivers. I will be running it in conjunction with our regular batch files. That is where I will be controlling the 'spooler' service.
Any help or direction would be greatly appreciated!
Cheers
===================== Edit section to contain changes empo suggested! I just wanted to add as well that the function "Remove-Spaces" is not my work! I found it on another website and incorporated it into my script. Original is here
$username=[Environment]::UserName
$machinename=[Environment]::MachineName
## this function removes all the blank spaces I had
Function Remove-Spaces {
param($target)
begin {
filter Do-RemoveSpaces { $_ -replace "\s *", "" }
}
process { if($_) { $_ | Do-RemoveSpaces } }
end { i开发者_如何学JAVAf($target) {$target | Do-RemoveSpaces} }
}
$printers=get-WmiObject Win32_printer -Property Name | where { $_.Name -like "\\printerserver1\*" -or $_.Name -like "\\printerserver2\*" }
$printers2=$printers | select-object Name | select-string -pattern "\\"
$printers2 > C:\testdir\"$username"PRINTERS.txt
$printers3=get-content C:\testdir\"$username"PRINTERS.txt | select-string -pattern "\\"
foreach ( $y in Remove-Spaces $printers3 )
{
write-output "RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n`"$y`"" | out-file *blah*
}
$printerdrivers=get-wmiobject -class Win32_PrinterDriver -Property Name
$printerdrivers2=$printerdrivers -split('=') #| select-string 'Name'
$printerdrivers3=$printerdrivers2 | select-string 'HP'
foreach ( $i in $printerdrivers3 )
{
$removeprinter=$i -split (',') | select-string 'HP'
#write-host $removeprinter
write-output "rundll32 printui.dll, PrintUIEntry /dd /c\\$machinename /m $removeprinter`"` /h x64 /v `"Type 3 - User Mode`"" | out-file *blah*
}
The output now looks like this:
RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n"\\printerserver1\ACC_PRINT_HP9040UPS"
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.1)" /h x64 /v Type 3 - User Mode
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.2)" /h x64 /v Type 3 - User Mode
rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Designjet T770 24in HPGL2" /h x64 /v Type 3 - User Mode
I believe that this will work... hopefully anyway. I am going to do some testing and I will report back and make sure to mark this as answered!
Scratch the out-file fix.. it doesn't work :( still need to do additional testing
thank you!
I'm trying to help you just on the first section of your script, about printers.
First, why are you redirecting printers into a text files?
## Get installed printers and create template command $printers=get-WmiObject Win32_printer -Property Name | where { $_.Name -like "\\printserver1\*" -or $_.Name -like "\\printserver2\*" }
with this you should have already all the printers.
Second, escape double quotes using backticks.
Third, try to use write-output
instead of write-host
.
foreach ( $printer in $printers ) { write-output "RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n `"$printer.name`"" }
If it does not work yet, append to the pipeline the invoke-expression
with out-null
:
foreach ( $printer in $printers ) { write-output "RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n `"$printer.name`"" | invoke-expression | out-null }
You can also try with invoke-item
.
精彩评论