r/PowerShell • • May 12 '20

Script Sharing I wrote a script and converted it to an exe file that we are able to put on our user's desktops that allows them to double click and run this. It allows them to see a list of our printers by floor and under each list is a map of that floor and where each printer is! Makes it easier for everyone :)

Post image
405 Upvotes

78 comments sorted by

17

u/fridgefreezer May 12 '20

With that many printers, would follow me print not be easier? I’ve got about 50, people print to the print queue and then either tap their id badge or type in a pin and the print comes out wherever they are.

Don’t get me wrong, good work with the powershell but I HATE printers and users using printers and follow me makes me HATE them slightly less.

7

u/Its_LoganWare May 12 '20

Oh you're absolutely right! But unfortunately working for a major company who as most know LOVE to do things the best way ;) makes it hard to convince the higher ups of the smartest way.

4

u/fridgefreezer May 12 '20

I feel your pain, I feel your pain only too well.

1

u/[deleted] Jul 03 '20

Wait so a badge in to print is off the rocker but your tiny little app with text boxes is the smartest way? Was this even done in Powershell? Could just be a super basic Vb.net windows form.

17

u/gangculture May 12 '20

share the script!

20

u/Its_LoganWare May 12 '20
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$formHome                        = New-Object system.Windows.Forms.Form
$formHome.ClientSize             = '650,300'
$formHome.text                   = "STL Printers"
$formHome.TopMost                = $false

$items                           = New-Object system.Windows.Forms.ListBox
$items.text                      = "listBox"
$items.width                     = 175
$items.height                    = 150
$items.location                  = New-Object System.Drawing.Point(20,46)
$items.Sorted                    = $true
$items.ScrollAlwaysVisible       = $true
$items.SelectionMode             = [System.Windows.Forms.SelectionMode]::MultiExtended;

$items2                          = New-Object system.Windows.Forms.ListBox
$items2.text                     = "listBox"
$items2.width                    = 175
$items2.height                   = 150
$items2.location                 = New-Object System.Drawing.Point(220,46)
$items2.Sorted                   = $true
$items2.ScrollAlwaysVisible      = $true
$items2.SelectionMode            = [System.Windows.Forms.SelectionMode]::MultiExtended;

$items3                          = New-Object system.Windows.Forms.ListBox
$items3.text                     = "listBox"
$items3.width                    = 175
$items3.height                   = 150
$items3.location                 = New-Object System.Drawing.Point(430,46)
$items3.Sorted                   = $true
$items3.ScrollAlwaysVisible      = $true
$items3.SelectionMode            = [System.Windows.Forms.SelectionMode]::MultiExtended;

$btnGetItem                      = New-Object system.Windows.Forms.Button
$btnGetItem.text                 = "OK"
$btnGetItem.width                = 100
$btnGetItem.height               = 30
$btnGetItem.location             = New-Object System.Drawing.Point(200,250)
$btnGetItem.Font                 = 'Microsoft Sans Serif,10'

$btntwoGetItem                   = New-Object system.Windows.Forms.Button
$btntwoGetItem.text              = "CANCEL"
$btntwoGetItem.DialogResult      = [System.Windows.Forms.DialogResult]::Cancel
$btntwoGetItem.width             = 100
$btntwoGetItem.height            = 30
$btntwoGetItem.location          = New-Object System.Drawing.Point(330,250)
$btntwoGetItem.Font              = 'Microsoft Sans Serif,10'

$btnMap1GetItem                   = New-Object system.Windows.Forms.Button
$btnMap1GetItem.text              = "1st Floor Locations"
$btnMap1GetItem.width             = 175
$btnMap1GetItem.height            = 30
$btnMap1GetItem.location          = New-Object System.Drawing.Point(20,200)
$btnMap1GetItem.Font              = 'Microsoft Sans Serif,10'

$btnMap2GetItem                   = New-Object system.Windows.Forms.Button
$btnMap2GetItem.text              = "2nd Floor Locations"
$btnMap2GetItem.width             = 175
$btnMap2GetItem.height            = 30
$btnMap2GetItem.location          = New-Object System.Drawing.Point(220,200)
$btnMap2GetItem.Font              = 'Microsoft Sans Serif,10'

$btnMap3GetItem                   = New-Object system.Windows.Forms.Button
$btnMap3GetItem.text              = "3rd Floor Locations"
$btnMap3GetItem.width             = 175
$btnMap3GetItem.height            = 30
$btnMap3GetItem.location          = New-Object System.Drawing.Point(430,200)
$btnMap3GetItem.Font              = 'Microsoft Sans Serif,10'

$People                          = New-Object system.Windows.Forms.Label
$People.text                     = "First Floor Printers:"
$People.AutoSize                 = $true
$People.width                    = 25
$People.height                   = 10
$People.location                 = New-Object System.Drawing.Point(25,19)
$People.Font                     = 'Microsoft Sans Serif,10'

$People2                         = New-Object system.Windows.Forms.Label
$People2.text                    = "Second Floor Printers:"
$People2.AutoSize                = $true
$People2.width                   = 25
$People2.height                  = 10
$People2.location                = New-Object System.Drawing.Point(225,19)
$People2.Font                    = 'Microsoft Sans Serif,10'

$People3                         = New-Object system.Windows.Forms.Label
$People3.text                    = "Third Floor Printers:"
$People3.AutoSize                = $true
$People3.width                   = 25
$People3.height                  = 10
$People3.location                = New-Object System.Drawing.Point(435,19)
$People3.Font                    = 'Microsoft Sans Serif,10'

$formHome.Controls.AddRange(@($items,$items2,$items3,$People2,$People3,$btnGetItem,$btntwoGetItem,$btnMap1GetItem,$btnMap2GetItem,$btnMap3GetItem,$People))

$formHome.Add_Load({
    $items.Items.Add("STL127")
    $items2.Items.Add("STL101")
    $items3.Items.Add("STL102")
})

do {
$btnMap1GetItem.Add_Click({Invoke-Expression -command "start-process '\\network location for first floor picture'"})

$btnMap2GetItem.Add_Click({Invoke-Expression -command "start-process '\\network location for second floor picture'"})

$btnMap3GetItem.Add_Click({Invoke-Expression -command "start-process '\\network location for third floor picture'"})
} until ([System.Windows.Forms.DialogResult]::CANCEL)

$btnGetItem.Add_Click({
    foreach ($item in $items.SelectedItems)
    {   
        if ($item -eq "STL127")
        {
             Invoke-Expression -Command "cmd.exe /c start\\network printer"

            msg /time:1 $env:UserName 'Success!'
        }}})

    $btnGetItem.Add_Click({
    foreach ($item in $items2.SelectedItems)
    {   
        if ($item -eq "STL101")
        {
             Invoke-Expression -Command "cmd.exe /c start\\network printer"

            msg /time:1 $env:UserName 'Success!'
        }}})

    $btnGetItem.Add_Click({
    foreach ($item in $items3.SelectedItems)
    {   
        if ($item -eq "STL102")
        {
            Invoke-Expression -Command "cmd.exe /c start\\network printer"

            msg /time:1 $env:UserName 'Success!'
        }
}
})

Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
msg /time:9 $env:UserName 'ATTN: To add a printer, click on the one you want followed by OK. To see where a printer is, click the location button under each list of printers. When you are finished adding printers, click CANCEL to exit.'
Start-Sleep 10

do {
$formHome.ShowDialog()
} until ([System.Windows.Forms.DialogResult]::CANCEL)

Removed extra printers to make it look a bit smaller but this is an outline of it all! Also replaced our network locations in this so you'd have to adjust the changes!

4

u/MostlyInTheMiddle May 12 '20

Another saved post I'll never get around to doing anything with. Good job.

2

u/danijeljw May 13 '20

This is excellent. Well done.

1

u/gangculture May 12 '20

nice one thanks! don’t know if i’ll need to use it but i love making things users can interact with. alas i’m not cut out to be a real programmer.

0

u/[deleted] May 12 '20

[deleted]

7

u/nascentt May 12 '20 edited May 12 '20

Microsoft devblog

TLDR: use & instead.

This is actually new to me, I was under the impression Invoke-Expression was better than & as it was a legit powershell command rather than just dumping the command into the shell

Note the main concerns - aside from complexity, is string injection.

0

u/[deleted] May 12 '20

[deleted]

3

u/nascentt May 12 '20

unless you're executing a command string

Ah I see. So.

Command

Will work without the ampersand

& "command"

Needs the ampersand?

I considered the variable with a debugger, but I felt that it wasn't as much of a risk as you need debugging privileges.

0

u/[deleted] May 12 '20

[deleted]

2

u/nascentt May 12 '20

Ah of course, I see what you mean. Just manually assigning the variable with the injection then resuming the script.

I thought you meant memory debugging, but you're right reassigning the variable works.

2

u/MyOtherSide1984 May 12 '20

Very neat tool! I'd love to implement this for our computer locations (although unreasonable to say the least). Does this feed off of a list somewhere or manually?

1

u/Its_LoganWare May 12 '20

It pulls from our print server! So it runs a command “\printserver\printername” and connects them. The pictures are also stored on the print server and if they click on that it’ll pull the picture from our print server as well.

2

u/Grant_Son May 13 '20

Nice 😁 Can I ask what you used to convert it?

2

u/Its_LoganWare May 13 '20

PS2EXE!

1

u/Grant_Son May 13 '20

Cool. When I last tired to compile a script with a GUI it has a melt down. Maybe I need to look at a different GUI method

130

u/DevinSysAdmin May 12 '20

I accomplish this by clearly labeling the printers themselves, and properly setting up printer GPOs.

32

u/da_chicken May 12 '20

We use secure printing and copiers as the only printers. You print to either the black and white or the color printer. Then you go to any copier, badge in, and release the job to your copier.

22

u/moca_steve May 12 '20

Good ole PaperCut

15

u/DaNPrS May 12 '20

PaperCut with follow me printing is indispensable for anyone with multiple sites/floors that travels. When we deployed it 3 years ago, the users looked at us like we were the devil;

"Are you serious?! We have to badge in to print?!?!"

After they got the concept we got nothing but compliments. If a printer is down, they just walk over to another and release there. And they don't nag you while you take sometime to replace the toner on the downed printer.

4

u/moca_steve May 12 '20

Printer goes down, jams, or is possessed by a gremlim, no worries - just walk over to the next one. It’s a really a software that proves its worth after implementation. And if really want to get a handle of costs the built in reporting for department charges / user charges is handy - If acct cares about that sorta thing.

1

u/Grant_Son May 13 '20

We still have multiple VIP users who demanded black and white desktop printers in their offices. Just because they can.

One of them even has the secure printer in the corridor outside their office, their small printer suits on a table about 4 feet from the MFD it's ridiculous

2

u/moca_steve May 13 '20

If the exec team isn’t leading by example it makes it a hard sell for the rest of the company to buy in. We all know MFD lower costs especially when scaled properly for monthly volume. I just hope that in your case they are able to support it initiatives and security policies by example.

3

u/Grant_Son May 13 '20

We are in a bit of an odd situation. Effectively a govt office so the corporate side are all on MFD's The other side is a free for all some see the up side. Some had that one time that they had to wait in a queue at an MFD for a few seconds or they one time the closest one was out of paper, probably with a spare box right next to it and so now refuse to use them

Still atleast we are almost rid of colour laser printers

9

u/danijeljw May 13 '20

I think the point here is the user is demonstrating a use for his research and study and is proud to show his skill set. Be a bit more kind when replying.

It’s all well and good to know that GPO is the answer, but it’s also nice to be encouraging AND helpful.

Please be kind.

3

u/EIGRP_OH May 13 '20

Don’t know why this is downvoted it’s just being considerate

2

u/danijeljw May 13 '20

Cos haters gonna hate. 🤷🏻‍♂️

4

u/EIGRP_OH May 12 '20

Lol every powershell thread someone’s gotta comment on how it can be done through group policy.

10

u/DevinSysAdmin May 13 '20

Yep and 3/4ths of the time that’s me, and the far majority of posters do not understand Group Policy and end up far down a rabbit hole. KISS.

-2

u/EIGRP_OH May 13 '20

But at this point this is a finished script, not how do I map printers with powershell? Where your answer would make more sense.

7

u/DevinSysAdmin May 13 '20

“We’re already $500 deep in the wrong solution, we couldn’t possibly go with a better one!”

2

u/Snickasaurus May 12 '20

This. Some don’t realize people might just be stretching their powershell wings.

6

u/EIGRP_OH May 12 '20

I understand there are better ways to do something sometimes but this is literally a subreddit dedicated to powershell, not r/sysadmin.

1

u/jebhebmeb May 13 '20

You can script gpo’s !

0

u/[deleted] May 12 '20

[deleted]

0

u/joerod May 13 '20

To be honest I found it easier to write a Powershell script and deploy a printer via SCCM than GPO. GPO is not as flexible IMO.

2

u/chandleya May 13 '20

If you’ve got sccm good for you.

1

u/joerod May 13 '20

Yep, if you don’t give Ansible a shot for certain more flexible than GPO.

1

u/5th-Line May 13 '20

Would you be willing to share? I’ve been trying to come up with a solution to push printers from SCCM.

0

u/[deleted] May 12 '20

[deleted]

5

u/mpd94 May 13 '20

If it was me it would be proximity based printer assignment. I'd lay out ble scanners all over the office and make some magic in the backend 🤪 and for the lazy users I'd make them run to the furthest printer on the last floor with a job retrieval limit of 2 minutes

3

u/dnwhittaker May 12 '20

I love these types of scripts because it expands my knowledge on how to use PowerShell and different ways to script. Thanks for sharing.

2

u/Its_LoganWare May 12 '20

Me too! I love to try to apply any scripting I can to different things just to make it more fun or “cooler”. It’s also fun to let the users interact with stuff. Make them feel like their own “IT Professionals” 😁

2

u/OrangeDartballoon May 12 '20

Upvoted mainly because some douche down voted but also because i support your statement. Great job on the script

15

u/get-postanote May 12 '20 edited May 12 '20

Just a quick UI/UX Note:

You could have made this a single box Listbox UI dialog.

One list box and a dropdown box to select a floor/location, have that single Listbox populate on the change event.

Also, if you publish the printers in ADDS, you could just pull those realtime, vs hard coding in your script.

https://devblogs.microsoft.com/scripting/weekend-scripter-easily-publish-all-printers-on-a-print-server-to-active-directory/

10

u/jbuk1 May 12 '20

Great idea but looking at those list of printer names makes me so glad we use follow you and paper cut. Go to any printer or copier, touch your staff badge and release your jobs.

3

u/Chaffy_ May 13 '20

That’s badass. Is it a scanner you can attach to any printer or do you have to buy the printer with it built in?

2

u/jbuk1 May 13 '20 edited May 13 '20

The printer needs to be able to run the papercut app for the route we've gone down.

This does require MFDs and additional papercut licenses so we have a few big high speed copiers in each building and then on each floor we have a number of smaller Sharp MXC301W with usb card readers dotted about.

Obviously not the cheapest route and we do still have the odd printer for senior management but that gets mapped through group policy as we have an OU for each office.

The idea is even spending more upfront on devices, we should save in the long run with follow you. (jobs not getting sent to wrong printer and printed multiple times, people printing and then not actually needed something, etc)

4

u/_benp_ May 12 '20

Your printer naming convention is terrible. You could solve the problem with a better naming system and no need for a custom app to help people select a printer.

3

u/bleedblue89 May 12 '20

St. Louis!

1

u/d3v3ndra May 12 '20

Could you share me how you convert it to exe.

3

u/get-postanote May 12 '20 edited May 12 '20

The tool used, does not really convert to a true '.exe', it's a wrapper/self-expanding zip file.

You can create '.exe' with PowerShell with help from C# and the like.

For a simple console example see:

https://devblogs.microsoft.com/powershell/how-to-write-a-console-application-in-powershell-with-add-type/

IMHO, the only real reason to do this is to prevent the everyday user from seeing source code and messing with it and those instances where you'd want the user to double-click to run vs using .bat/.cmd/.vbs file in concert with .ps1.

Any skilled person can still get at the code behind that PS2EXE approach since the code has to be expanded, loaded into memory (plain text) before it does anything. So, don't be putting PII or other sensitive info in scripts, period.

1

u/d3v3ndra May 12 '20

You are omipresent, thanks very much for the explanation. Keeping rocking

2

u/get-postanote May 12 '20

No worries, I'm old and gray and ready for those relaxed golden years (well, you know... all things considered), but I do what I can. ;-}

2

u/deanfx May 12 '20

This should help get you in the right direction:
https://github.com/MScholtes/TechNet-Gallery/tree/master/PS2EXE-GUI

This works, I also use ISESteroids but that's paid, and also requires you use ISE which...you know...ISE meh. PS2EXE is the way to go.

1

u/d3v3ndra May 12 '20

Checking, thanks

2

u/_MSPisshead May 12 '20

You know what, good for you. I think this is cool as all hell and wish I had done it honestly. Ignore the negative comments here

2

u/TheOther1 May 12 '20

Anyone remember Novell iPrint?

2

u/[deleted] May 12 '20

I just realized another business that is going to be hit even harder by WFH. Bye bye worthless printouts handed out in meetings!

2

u/robvas May 12 '20

You could have made a map/web page :)

2

u/gostega May 13 '20

Nice script. Funny coincidence, your printer asset naming convention is the same as our STaff Laptops (STLxxx).

2

u/PinchesTheCrab May 13 '20

But what about the rooftop printers?

2

u/chongtxtx May 12 '20

looks cool as hell!

2

u/dhakhoa93 May 12 '20

Whoa that's a lot of printers!

4

u/jstar77 May 12 '20

Users gonna print and they aint walkin across no hallway to pickem up.

1

u/elevul May 12 '20

We just use SafeQ, the user goes to any printer, badges and prints.

1

u/Geminii27 May 13 '20

Next step: allow them to select a printer and set it as their default?

1

u/[deleted] Jul 03 '20

I think you need a new naming convention for your printers. Try something like

[ID]-[Floor] STL001-1

Or STL008-3

1

u/static_startup Jul 26 '20

What program are you using for conversion from PS1 to exe?

1

u/[deleted] Jan 25 '24

WTF? People are still printing? Losers

0

u/[deleted] May 12 '20

[deleted]

2

u/Its_LoganWare May 12 '20

Yeah I just used PS2EXE and it worked nicely! And awesome thank you! I'll make the adjustment

1

u/Lee_Dailey [grin] May 13 '20

howdy SaitamaSayingOk,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee

0

u/[deleted] May 12 '20

[deleted]

1

u/Its_LoganWare May 13 '20

I totally agree :( That’s my next project.. gotta love the effort that person put into naming these :,-)

0

u/[deleted] May 12 '20

[deleted]

1

u/Its_LoganWare May 12 '20

It's how they had set up the printers before I was ever here :/ Not personally a fan of how they did it but eventually we will get the names fixed. They should be named according to a floor but :,-)

1

u/BadSausageFactory May 12 '20

I actually wiped the comment, came back and it seemed a little snarky

but yeah holy shit who set that up, was at one place where a cable company did the runs, labeled each jack.. and then randomly punched them down at the block. I just feel sorry for anyone that comes in after me and hope they don't think I'm the asshole.