r/PowerShell • • Jan 13 '20

Network Printer Driver Nuker - Deletes all network printers & their drivers, then remaps the printers

https://github.com/Justin-Lund/Printer-Remapper
221 Upvotes

47 comments sorted by

View all comments

Show parent comments

3

u/Dsraa Jan 14 '20

Why Shure! I wanted to perfect it up a little more, as its a bit rough around the edges but I'll share the relevant snippet of powershell. There's a few ways to pare the results to get the desired result, so I'm sure this can probably be modified/perfected to fit your scenario:

$PrintSRV = Get-ADObject -LDAPFilter "(&(&(&(uncName=*)(objectCategory=printQueue))))" -properties *|Sort-Object -Unique -Property shortservername #| select distinguishedname, shortservername

$PrintSRV.Distinguishedname.Split(',')[-3]

$PCOU = Get-ADComputer "$env:ComputerName" -Properties *

$PCOU.Distinguishedname.Split(',')[-3]

foreach($srv in $PrintSRV){

if( $PCOU.Distinguishedname.Split(',')[-3] -like $SRV.Distinguishedname.Split(',')[-3]){

write-output "Server: $srv.shortServerName.split('=)[-3]"

$Prnlistbox = $srv | select shortservername, location

$Prnlistbox

[void] $listBox.Items.Add("Print Server:$srv.shortServerName.split('=')[1]")

}

#Else{Write-Output "No Match"}

}

Hope this gives some ideas.

2

u/MartianMoon Jan 14 '20

Awesome, thank you!

1

u/Lee_Dailey [grin] Jan 15 '20

howdy Dsraa,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th from the left hidden in the ... "more" menu, & looks like an uppercase T in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces

  • prefix each code line with 4 spaces

  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

the easiest way to get that is ...

  • add the leading line with only 4 spaces

  • copy the code to the ISE [or your fave editor]

  • select the code

  • tap TAB to indent four spaces

  • re-select the code [not really needed, but it's my habit]

  • paste the code into the reddit text box

  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

2

u/Dsraa Jan 15 '20

Hi Lee, thank you for the explanation.

I most likely will be uploading it to either paste bin or GitHub once I am done messing with it. I realized that what I had posted was a version I had been tweaking the other day, and was not the desired formatting I had wanted.

I also realized I had left out the section that queries the selected print server and loops through and installs each printer. So I will be posting a link here at some point.

1

u/Lee_Dailey [grin] Jan 15 '20

howdy Dsraa,

you are most welcome! glad to help a tad ... and i look forward to seeing your updated code. [grin]

take care,
lee