r/PowerShell • u/DragonToutNu • Aug 06 '20
I've created my first practical script and I felt like a king
Hello all, I just wanted to tell someone that today I've created my very first script with powershell.
What really made me happy is that I just started learning powershell. I'm currently reading "Learn Windows PowerShell in a Month of Lunches" and only at chapter 8.
I was given an excel sheet with about 350 user who I needed to update the job title and their manager.
I immediately realized that I could create a script soemthing with powershell to have it done in one go.
I used get-help on every single lines. Google the shit out of it and had to troubleshoot by excel document.
Whoever build that list made it really hard to automate. First they gave a column with the name of the user: Lastname, FirstName They also gave me the name of the new manager: FirstName LastName or FirstName, LastName or Lastname, FirstName
It was quite a puzzle but in the end I managed to modify the excel sheet to order the names to use the displayname to find them in AD and by creating a new column using the first letter of the first name + last name of the manager to use as username and update the parameter -manager.
Wasn't easy, but its only the beginning!
34
Aug 06 '20
This actually the best way to learn. Reading will only get you so far. Applying what you’ve read is your first step to mastery and you’ve just taken that step. Rock on fellow nerd.
9
u/uptimefordays Aug 06 '20
The reading gives you an understanding of what something can do or what common pitfalls might be, actually going out and practicing--that's how you get really good. It's important to remember practice doesn't make perfect, perfect practice makes perfect. Reading books is a good way to learn best practices.
-4
Aug 06 '20
Not sure what the point of this reply is?
12
u/Alexbeav Aug 06 '20
The point is that blind practice without knowledge behind it can grow into practicing inefficiently or develop bad habits. Just reading will also not cut it, you need to do both.
2
u/Pacman042 Aug 06 '20
That describes a lot of my ability at the moment. Being too young to care for the studying and jumping right in has left me with tons of bad habits/knowledge gaps. I'm getting better but it's taken a lot longer than it should.
1
Aug 06 '20
Which is why my comment literally says ”Applying what you’ve READ is the first step to mastery”. No one ever said don’t do both.
1
3
Aug 06 '20
Not only doing it practically but taking on a pretty difficult task I think is the best way to learn. Not just copy pasting a solution but problem solving to achieve a specific goal which isn't straight forward.
8
u/LogMonkey0 Aug 06 '20
Good job!
This is why i try to do everything with powershell when possible, you will become more versed in the language and will be building a toolbox before you even know it!
3
u/CMTraceBeaulieu Aug 06 '20
This right here. First thing I do now is Google whether my problem can be fixed with PowerShell. Next thing you know, you're remembering how to do things off the top of your head.
6
u/poshmosh01 Aug 06 '20
Rock on, learning to do things a little faster and more efficient is always good
Plus you learn more about ad/powershell at the same time!
6
u/zrv433 Aug 06 '20
Good job. Next time you have non standard name data, think about using the ANR attribute with an -ldapfilter query. More about how it works here https://social.technet.microsoft.com/wiki/contents/articles/22653.active-directory-ambiguous-name-resolution.aspx
2
5
4
u/Cyberhwk Aug 06 '20
5
u/DeafLoaf Aug 06 '20
Change the error colour to green, they're not errors, they're opportunities to handle issues correctly.
4
u/gordonv Aug 06 '20
This is awesome. I think we all have little stories where we make a script and it just lifts a huge burden off our shoulders.
There's a magic and secret about this. What you just learned translates to all programming languages!
So... I suggest, after powershell, you look @ r/cs50 and do the free course. It's a nice guided tour through a lot of hard things made simple. :)
4
u/Crully Aug 06 '20
Soon you'll be like the rest of us. Have a 20 minute job, but spend an hour writing scripts, modules, and tests to complete it satisfactorily.
3
3
3
3
u/Wooden-Tart Aug 06 '20
You are a king, you have taken the first few steps of mastering a computer. We use computers everywhere. E V E R Y W H E R E. Deep Space? Yes. Deep Ocean? Yes. In your chest? Yes.
Are you a king? Yes.
3
u/DeafLoaf Aug 06 '20
Well done, I know seasoned Admins that refuse to open PowerShell.
2
u/todayyou500 Aug 06 '20
Wait really? All the knowledgeable IT staff I knew did some sort of scripting whether it be simple bat files, python , c, powershell or some azure/docker/kuber backend stuff.
Didn't even need to be great at it, just effective at what you need getting done.
4
u/DeafLoaf Aug 06 '20
Yeah it's a joke, logging on to do tasks interactively few servers at a time to me got old years ago. They actively suggest that approach. It's mind boggling.
5
u/Joejoe930117 Aug 06 '20
At an old job of mine, there was me and one other sysadmin. The building was being put on some sort of power backup, and we needed to bring down the virtual environment briefly. About 150 servers. He said he'd be coming in a couple hours early to shut them down, one at a time. I told him "I could, in a single line of powershell, have that done automatically for you."
His response?
"No thanks. I'd rather do it manually."
To this day, I cannot even form a proper rationalization nor response to that. This was years ago.
3
3
u/adbertram Aug 06 '20
Congrats! Keep going! That feeling of accomplishment will only increase over time.
3
u/copper_23 Aug 06 '20
I used get-help on every single lines. Google the shit out of it and had to troubleshoot by excel document.
This! this is the best way to learn so you can get a feel of the errors and the scripting process, when you're reeaaaallyyyy comfortable with the script language, you might do a whole script before testing and it always feels like "Holy crap, I can't believe that worked"
3
u/cbass377 Aug 06 '20
This feeling right here, and the knowledge that the next time this happens, you won't have to deal with it is the true greatness of scripting.
On my scripts, I write them so that if you just run the script without specifying a parameter (could be anything), it prints out a help screen.
script1.ps1, script1.ps1 -h --> print help
script1.ps1 -AnythingElse --> do it.
This helps you when you have to run this script in 18 months and don't remember what it does.
Congratulations though, enjoy this moment and don't undervalue this script, because it seems to do something simple. These are the best scripts, from now on, you have turned a 4 hour task into a 4 minute task (1 minute to review the spreadsheet for special characters like O'Rielly and format changes, and 3 minutes to get a fresh cup of coffee while the script runs), and this kind of thing pays off for years.
3
2
u/Nanocephalic Aug 06 '20
I’m proud of you! Before you know it you’ll be a grumpy old scripter like the rest of us :)
2
u/Penny_Farmer Aug 06 '20
Great job! The absolute best thing you did on this exercise is using Get-Help. As you become more advanced you’ll forget a lot of Powershell; but if you know how to really explore Get-Help, you are golden!
2
2
u/Stiefeljunge Aug 06 '20
Great idea to ask the DC.
Have fun automating as much of your daily work as you can, I started at my current job two years ago and opened my first powershell session in my first week here.
Now the only thing I have to get up for is getting coffee.
2
Aug 06 '20
Good job mate!
I started venturing into PowerShell and SQL and PHP.. No effing idea how to SQL or PHP anything, but learning and doing is the best feeling of satisfaction.
Virtual elbow tap, don't want to get covid bwhahaha
2
2
u/Flashy_Ideal Aug 06 '20
Good job! They'll be more feelings like this. My advice would be to take smaller tasks to something like this as well, make sure all your solutions scale :)
2
u/MyOtherSide1984 Aug 06 '20
Nice work! I've sometimes found that it's easier to flex my Excel skills on a list prior to feeding it to PS as I know a little bit more about how to sort and filter out in there and it's quicker. It's excellent that you were able to get it all working in one go though! It feels amazing every time I'm able to use PS to do something that no one else thought to do with it (at work)
2
u/yourenotwurvy Aug 06 '20
Congrats, great feeling and a great way to learn by applying to a real world problem.
2
u/wrootlt Aug 06 '20
I know the feeling. Not just with PowerShell. I remember i found a very small visual glitch (checkbox overlapping with another one) in a Java application, found this place in the code, googled how to change coordinates of objects in Java, submitted my PR and it was merged. Felt like "OMG, i am a developer!" :D
2
u/Polyolygon Aug 06 '20
Keep at it! I’ve been using it for the past 3 years, and when you get a task that would take days to do manually in AD, Powershell has come through and turned my AD tasks and changes into mere minutes(including coding time).
2
2
u/IntellectualKittens Aug 06 '20
Couldn't you have given the sheet back to the person and have them order the names correctly so you can process it? I would have. :)
2
u/Flyboy Aug 06 '20
The Active Directory cmdlets are a pathway to many Powershell abilities some consider to be ... unnatural.
2
u/Material-Shoulder-88 Aug 07 '20
I was given an excel sheet
This is always how it always starts. Someone hands you a terrible file format and then you build something to bypass having to deal with it.
1
u/AutoModerator Aug 07 '20
Sorry, your submission has been automatically removed.
Accounts must be at least 1 day old, which prevents the sub from filling up with bot spam.
Try posting again tomorrow or message the mods to approve your post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Lee_Dailey [grin] Aug 07 '20
howdy DragonToutNu,
welcome to the addictive coding club! [grin]
take care,
lee
1
1
0
Aug 06 '20
[deleted]
3
u/DragonToutNu Aug 06 '20
I'll have to comment it as I haven't commented anything hehe.
I also need to add a little functionality which is to log the errors that are coming out (failed changes)
3
u/uptimefordays Aug 06 '20
Also worth commenting out lines that didn't work so you can see what you tried--while ya don't need to include that on final production versions of scripts it can still be handy on your personal versions or in early commits.
3
u/shauntau Aug 06 '20
This! I comment out the lines that didn't work for that reason too. Also, so that if it would have been easier to do it that way, and the functionality happens later, I can try again. I then comment WHY it didn't work as well.
2
u/uptimefordays Aug 06 '20
Yep having in context notes about what did and didn't work is super helpful. I don't ship code with such comments but I've definitely got 'em!
2
u/shauntau Aug 06 '20
yeah, some programs can keep your documentation so you can remove the comments for production. I can remember if VS code will do that, but I have used one before that did that.
I could take those comments and turn them into more user friendly directions. It was great.
3
u/uptimefordays Aug 06 '20
I usually phase the comments out with subsequent commits--at least in private repos. My public repos are generally the same but with comments about what worked/didn't work/etc. moved to git commits.
49
u/[deleted] Aug 06 '20
ALways a satisfying feeling when your starting out to get something actually productive achieved with Powershell.