r/PowerShell • • Oct 09 '19

My IT Tool

Figured I'd share my little program as well. Been working on this on and off for about 6 months now. Just started getting into Powershell. Some key features are a security check the second the program is ran. If the user is not part of the proper OU, it will display an error message, email the IT dept with their username and computer, and then close the program. Just to make sure only IT people can use it. Every action is also logged to a central server Event Viewer.

Program screenshots: https://imgur.com/a/qsjk4ek

Code: https://pastebin.com/0FmdSaRF

I know I can clean it up some and probably stream line some stuff but, again, I've only been using Powershell since the beginning of the year basically and I have no real other programming experience prior. I made this to really just make my job a little easier and quicker. I started this in Powershell Studio trial with the form and tabs. Pulled that code and have been using ISE ever since. Apparently Studio is just too expensive for my company but oh well.

EDIT: GOLD?! Wow, my first! Thank you internet stranger! I also wanted to thank everyone on here who helped me the times I came looking for help with certain aspects of this thing. I really appreciate it. And thanks to everyone on this post for tips and suggestions, I will definitely look into them all. Always looking to improve.

172 Upvotes

62 comments sorted by

35

u/MallocArray Oct 09 '19

Visual Studio Code is free and has a lot of nice features over ISE.

I also learned a lot from the "Learn Powershell Scripting in a month of lunches" book as a followup to the first Powershell book.

9

u/flatmelvin Oct 09 '19

1

u/bajinabass Oct 10 '19

Thanks, I will definitely check that out.

12

u/helmyr32 Oct 09 '19

+1 this

There are ways you can set up VSCode to behave pretty much identical to ISE. (google knows how) If I remember correctly ISE is also considered obselete. Personally, I only ever use ISE on servers for quick stuff.

I prefer using Atom, but VSCode is amazing as well and has better PowerShell support.

3

u/Draphick Oct 10 '19

Im an avid Atom user too, but lately it’s been less powershell and more python for me. We’ve got this decent battle between atom and vscode users at work. And then the one lonely dude who’s all about his Notepad++. Lol.

2

u/bajinabass Oct 09 '19

I have that book myself. I also have VSCode but it was a little much for me honestly. I am not really a programmer.

18

u/hellphish Oct 09 '19

I'm not really a carpenter but when I need to hammer a nail I still use a hammer. VSCode is a great tool no matter your level of programming competency (which is probably higher than you think, your tool looks great)

7

u/bajinabass Oct 09 '19

Thank you!

5

u/noxzobax Oct 09 '19 edited Oct 09 '19

Use the mahapps plugin to do a better interface 😉 https://mahapps.com/controls/tab-control.html

1

u/bajinabass Oct 10 '19

That looks interesting. Thanks!

5

u/ipreferanothername Oct 09 '19

i used to use N++ because i worked with an app that had tons, tons of config and text log files i was in all day and my posh-fu was weak to middling. using VSCode will, pretty quickly, spoil you and then using something else will sort of bite.

10

u/zw9491 Oct 09 '19 edited Oct 10 '19

Wait. What. “Get ... Password”? That shouldn’t be able to happen.

Edit: okay... I guess plain text passwords in a column it is then

$MTPQuery = Invoke-DbaQuery -SqlInstance "Server" -SqlCredential $creds -Query "Select password from TABLE where UserName = @Username" -SqlParameters @{ Username = $Login} | select -expand password

Edit2: uhhhhh

$pw = get-content C:\PW.txt Edit 3: okay, being stored with DPAPI, 2 is mitigated at least

1

u/bajinabass Oct 10 '19 edited Oct 10 '19

Internal communication program that was custom built in house by our Director. So I'm just pulling from his db. And the $pw is from what I read about using stored encrypted credentials. If there is a better way, please let me know.

Edit: This is what I ran to make that pw.txt file:

$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content c:\pw.txt

Referenced from here: https://interworks.com/blog/trhymer/2013/07/08/powershell-how-encrypt-and-store-credentials-securely-use-automation-scripts/

3

u/zw9491 Oct 10 '19

Passwords stored in a column was more a commentary on whoever set that up. That’s an absolutely horrible way to do it. If those are available, might as well use them. I’m assuming y’all were doing the same process manually, so nice job automating that shrugs in cyber security. I’m sure it’s some super business critical app that can’t just be replaced... so yeah....

Jumped the gun on the pw.txt.... got my functions confused with the naming/function of “ConvertFrom”. That’s the best way I know of at least without setting up some sort of password repository and grabbing it from there

2

u/bajinabass Oct 10 '19

Oh I totally agree it's a terrible way to hold passwords. But it is the brain child of the Director and his program. So I just went with what I could do in regards to that.

12

u/mystonedalt Oct 09 '19

Almost looks like you got holdta some of the shit I wrote as a consultant! Good stuff, mate.

6

u/bajinabass Oct 09 '19

Thanks! Curious, which part(s)? I definitely took the color scheme from another post on here a while back. Just really liked the way it looked. For Excel output I went and looked up how to do it and just modified some code from a site to do what I needed within my system. The event logging I just added yesterday actually after seeing someone mention it on another help desk tool post.

3

u/mystonedalt Oct 09 '19

Nothing specific, I've just written about 500 POSH GUIs to hand off to Operations teams when I'd go in to help unfuck their org.

3

u/bajinabass Oct 10 '19

I kinda did the same thing for our order processing department. They have these little issues that they are always running into so I got a little tired of the tickets and made them a one form GUI that answers any one of their questions. Tickets have drastically reduced.

1

u/stuartall Oct 10 '19

Hey so I know there’s a few posts about GUIs recently from Adams stuff. Any particular tools you prefer for GUIs ? All these make we want to re-jig some of my Helpdesk scripts.

6

u/edelwater Oct 09 '19

nice! for config instead in the file itself as parameters you could use an ini file and put there the variables: https://github.com/projectje/powershell_scripts_various/blob/master/Config/Config.psm1

For reading / writing eventlog you could move code to a module e.g. https://github.com/projectje/powershell_scripts_various/blob/master/EventLog/EventLog.psm1

once you have everything in modules you can keep the main script very small with only a couple of variables and then leverage on the modules.

3

u/bajinabass Oct 09 '19

Oh wow! I will check both of those out. Thanks for those links.

2

u/edelwater Oct 09 '19

also: https://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/ (windows credential manager for storing credentials instead of c:\PW.txt ) (or search this reddit for cmdkey)

2

u/bajinabass Oct 09 '19

I will check that out. Thanks!

3

u/Slashj Oct 10 '19

I like to use region in my GUI Scripts, so its easier to get to those points you might wanna change at some point.

Simply wrap the code for every button or bigger element into:

#region Button1

#endregion Button1

You can also indent those regions for even more visability.

CTRL+M also opens and closes all regions, atleast in ISE.

2

u/bajinabass Oct 10 '19

That's not a bad idea at all. Thanks for that!

5

u/redditors_r_manginas Oct 09 '19

Noob here, does PowerShell support GUIs natively?

3

u/bajinabass Oct 09 '19

You have to build the GUI but yes. There are many programs and options to do it. I started with the Powershell Studio trial to just get my form slightly built (it's limited to 5 objects if I remember correctly). Then I just brought the whole thing over to ISE and have been using that ever since. It's rather annoying positioning everything but other than that, it's been quite the positive learning experience for myself.

4

u/Aggrodisiakum Oct 09 '19

More or less. You can use .net objects to create wpfs or winforms.

2

u/redditors_r_manginas Oct 09 '19

I wonder if I could create scripts to show to my boss to persuade him to use PowerShell. A GUI would be much easier to push.

4

u/Reverent Oct 09 '19

If you have the authority to push a web service, you can set up Jenkins to create web forms and run PowerShell scripts remotely.

My adoption rate of my PowerShell tools was basically zero until I gave them a handy website to go run them on.

2

u/EiKall Oct 09 '19

Also gives you privilegue separation. So you can keep the number of people with lots of rights down to the minimum. Give the right to your backend on a server and only let them send requests to your tool.

2

u/rumorsofdads Oct 10 '19

I use free style jobs with Jenkins, but displaying output to the end user is my issue that I haven’t understood how to implement in Jenkins, unless teaching them where to click to see console output is that answer.

2

u/Reverent Oct 10 '19

It's not a perfect solution, but it ticks the boxes that I consider essential:

  • AD Integration

  • Build (job) audit logs

  • Git integration

  • web forms for parameters

  • separation of privelege (unpriveleged users can run priveleged tasks)

What it is missing is:

  • responsive forms (being able to run a script to gather info, and then make decisions based on that info)

  • user friendly console output

  • sexiness (adding a modern theme helps, but it's not a pretty program).

In terms of functionality over form though, it certainly does the job. If I need prettier controls, there's Powershell Universal Dashboard (advanced features are pricey) or just straight up code in electron.

3

u/artemis_from_space Oct 10 '19

We go through Universal Dashboard, among others a self service portal for users to reset passwords etc.

Just a day ago I made a page to allow people to upload a csv file containing serialnumbers/macaddresses to check in different systems if the machine is registered and to who.

It also checks some log files with old machines that have been removed from other systems.

This used to be a manual process, ie get a document (if you are lucky, those that were unlucky got a paper copy) and then login to system a, check if the machine is there, if its not, log in to system 2, if its not there, then log in to system 3, etc... repeat for each serial number. Now it takes a few seconds if the cache from all the systems are done or up to two minutes to get all the data...

2

u/nj12nets Oct 09 '19 edited Oct 09 '19

Yes but you can start small with text box prompts for the variables you need filled in procedures you run one at a time. For things like onboarding or terminations, if you routinely perform more than 5 at a time I'd think youd be better off using csv files to specify the variables and let it run everyone listed in the file. Building a gui is cool too and tools like poshgui.com. theres also a plugin/extension for viscose that can automate gui creation by setting up the parameters and running their automated command to generate the gui. I've been busy on other projects but I had gotten up to modifying the script once the gui was created to make sure it does everything needed. The only thing is getting the button clicks execute what you want with the variables entered into the text or combo boxes.

I've only started using powershell in the last 6 months or so and love it. I've started automating or using scripts for even simple repairs so they can be done automatically. Even just clearing corrupt Outlook profiles I can run a script that deletes the reg key and adds a new reg key to create a new one as a default profile so the user just has to login when they reboot (which is also scripted).

1

u/len_sam Oct 10 '19

Even just clearing corrupt Outlook profiles I can run a script that deletes the reg key and adds a new reg key to create a new one as a default profile so the user just has to login when they reboot (which is also scripted)

Would you be able to share this code?

1

u/HairyHope Oct 09 '19

If you haven't looked into it yet, I'd suggest giving POSHGUI a try. It will give you a nice WYSIWYG space to build your Powershell GUI stuff.

2

u/BluBallz Oct 09 '19

Do you store user passwords in plain text in an AD attribute and SQL?

1

u/bajinabass Oct 10 '19

Just the one AD attribute for a in house program that we use. It doesn't even have internet access so that's why we just put it there. Director's idea so......the SQL is for our internal portal for communications. Also built in house to save money.

2

u/[deleted] Oct 10 '19

This is awesome.. so the password fields let you query and find out the password for an account?

3

u/bajinabass Oct 10 '19

Yes. But it is restricted to our internal custom systems and their databases. The only exception is our email passwords. We use a hosted system but keep internal records of each user's password. Helps with our outside sales force who tend to break their phones alot and not remember their passwords. And we made the change to go really random with their passwords to increase security a little. They hate it since they can't just use normal words but I like not getting hacked more.

2

u/[deleted] Oct 10 '19

Awesome, I want something at work too. While I have everyone’s passwords, it would stop all the confusion sometimes

2

u/ttech24 Oct 10 '19

Nice

1

u/bajinabass Oct 10 '19

Thank you. Honestly I was a little hesitant to share cause I figured I would get crucified for something that I just didn't know. But thanks to everyone on here, I've been able to fix and improve a lot in this thing.

2

u/gjpeters Oct 14 '19

I'm so glad you did share. If/when I share things I look forward to the crucifixion. It's never as bad as you think and, for me at least, it's how I learn fastest.

2

u/frogworks1 Oct 10 '19

Very cool project!

1

u/bajinabass Oct 10 '19

Thank you!

3

u/yorii Oct 09 '19 edited Oct 10 '19

It will do a check to see if WinRM is enabled on the remote computer first. If it isn't, the function quits and an error will pop-up notifying the user that it is not enabled.

You could probably use this:

Invoke-WmiMethod -ComputerName $Computer.Name -Namespace root\cimv2 -Class Win32_Process -Name Create -ArgumentList "winrm quickconfig -quiet" -AsJob
Invoke-WmiMethod -ComputerName $Computer.Name -Namespace root\cimv2 -Class Win32_Process -Name Create -ArgumentList "PowerShell -ExecutionPolicy Bypass -Command Enable-PSRemoting -Force -SkipNetworkProfileCheck" -AsJob

3

u/bajinabass Oct 10 '19

Wow, thank you for that! I just implemented it in my catch statement. It now let's them know that WinRM is not enabled and it will attempt to install it. My initial test installed fine. Now I just need to figure out how I can work it so it does it in another try catch and report back successful or not.

1

u/Lee_Dailey [grin] Oct 10 '19

howdy yorii,

it looks like you used the New.Reddit.com Inline Code button. it's 4th 5th from the left hidden in the ... "more" menu & looks like </>.

on Old.Reddit.com, the above does NOT line wrap, nor does it side-scroll.

for long-ish single lines OR for multiline code, please, use the Code Block button. it's the 11th 12th one from the left, & is just to the left of hidden in the ... "more" menu.

that will give you fully functional code formatting, from what i can tell so far. [grin]

take care,
lee

2

u/yorii Oct 10 '19

ah thanks, I was wondering where they hid that button...

1

u/Lee_Dailey [grin] Oct 10 '19

howdy yorii,

you are very welcome! glad to help reveal the hidden powers of reddit ... [grin]

take care,
lee

1

u/captain5260 Oct 10 '19

Are you a cop? Lol. JK good auditing and Op Sec. Will check it out.

1

u/bajinabass Oct 10 '19

If I was I'd have to tell you........Thanks!

1

u/bradowatson Oct 09 '19

Hey bro, what's your Passsword?

1

u/edgy_brah_911 Oct 10 '19

cool socks.

0

u/[deleted] Oct 09 '19

LMAO points on the name... "Tool"

3

u/MalletNGrease Oct 09 '19

He obfuscated it. Probably has a company name in it.

1

u/[deleted] Oct 09 '19

Yeah. But tool is so much better anyway.