The Flat Earth Society

Other Discussion Boards => Technology & Information => Topic started by: Fortuna on July 04, 2021, 06:35:08 AM

Title: On the notion of Powershell
Post by: Fortuna on July 04, 2021, 06:35:08 AM
I have to turn this in for a class, and I just wanted to see if any wizards here could give it a quick once-over. I'd feel bad if you spent a long time on it, so please don't do that. But it is probably a very simple script anyway. Although, it took me a good 5 hours to do. The individual lines are no problem. I understand what they do just fine on their own, but I'm having trouble understanding switches and how the entire thing works as a whole. I ran it in ISE and it seems to work, although probably in a ghetto as hell way.

do {
$input = (read-host "Please enter options '1', '2', '3', '4', and '5' in order.");

switch ($input) {
    1 { "Outputting .log file list in Test1 folder to 'DailyLog.txt'"; get-date | out-file -filepath $psscriptroot\DailyLog.txt -append
        get-childitem -filter *.log | out-file -filepath $psscriptroot\DailyLog.txt -append }
    2 { "Outputting file list in Test1 folder to definitelynotavirus.txt"; get-childitem $psscriptroot | format-table | out-file -filepath definitelynotavirus.txt }
    3 { "Getting CPU time and memory usage"; get-counter -counter '\processor(_total)\% processor time' -sampleinterval 5 -maxsamples 4
get-counter '\memory\committed bytes' }
    4 { "Getting system processes"; get-process | select-object -property cpu, name | sort-object -property cpu -descending | out-gridview }
    5 { "Ending the script"; break }

}
} until ($input -eq 5)
Title: Re: On the notion of Powershell
Post by: Lord Dave on July 04, 2021, 08:43:23 AM
Looks fine.

Switches are basicaly nested If... Else... Statements in an easy to read format.
If (input -eq 1)
(Code)
Else if (input -eq 2)
(Code)
Else if...

Also you should put something for all other input, like an error message.
Title: Re: On the notion of Powershell
Post by: xasop on July 04, 2021, 08:52:15 AM
Also you should put something for all other input, like an error message.
If the objective were to design a good UI, the commands would be named something more expressive than "1", "2", "3", "4" and "5". This is probably good enough for whatever Computers 101 assignment it's for.
Title: Re: On the notion of Powershell
Post by: Lord Dave on July 04, 2021, 08:56:55 AM
Also you should put something for all other input, like an error message.
If the objective were to design a good UI, the commands would be named something more expressive than "1", "2", "3", "4" and "5". This is probably good enough for whatever Computers 101 assignment it's for.

Probably but doesn't hurt to learn good coding practices early.
Title: Re: On the notion of Powershell
Post by: Fortuna on July 04, 2021, 09:54:08 AM
Looks fine.

Switches are basicaly nested If... Else... Statements in an easy to read format.
If (input -eq 1)
(Code)
Else if (input -eq 2)
(Code)
Else if...

Also you should put something for all other input, like an error message.

Oops, yeah I forgot that last part. Thanks. The assignment was to write a script for someone who has no knowledge of powershell or console commands and they're just supposed to run it and press buttons, so I guess I wrote them like "1" because I thought it would make it more obvious. But yeah, it looks pretty janky so I'll change it.
Title: Re: On the notion of Powershell
Post by: Pete Svarrior on July 04, 2021, 12:54:19 PM
I really don't like your solution to exiting the script. Effectively, command number 5 does nothing (the break statement is completely useless, unless you expect that some inputs will match more than 1 case - in which case you should have breaks at the end of every option), and the script only ends because of the condition in your loop. It works, but it's a bit shitty, because you can't just look at command 5 to see what it's actually doing. In a much larger script, this might be difficult for someone trying to understand your code to properly trace. Personally, I'd have it exit rather than break in line 11. You can then use a cleaner-looking (imo) while($true) loop instead of do { ... } until(...)

As Dave suggested, I would also add a default case to your switch statement - if the user tries to choose an option that's not 1-5, they could/should be reminded of their choices.

xasop's point is also good - give an actual description of what each number does before the user has to choose them - otherwise they're forced to try everything to know wtf each option does.

Finally, try to get into the habit of indenting your code consistently. You have 1 level of indentation inside the switch block, but nothing inside the do block. Something like:
Code: [Select]
"Welcome to Pete's great script."
while($true) {
    $input = (read-host "`n1 for farts`n2 to get outta here`nChoose your option:");

    switch ($input) {
        1 { "==========`nhaha farts`n==========" }
        2 { "bye"; exit }
    }
}
is more readable, because you can visually see which code is in which block.
Title: Re: On the notion of Powershell
Post by: Pete Svarrior on July 04, 2021, 01:15:59 PM
Oh, also, this documentation on switch statements should be very helpful both in understanding the concept, and all the weird and wonderful things you can do with it: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch
Title: Re: On the notion of Powershell
Post by: Fortuna on July 05, 2021, 03:31:46 AM
Hmm, yeah. I just got the assignment back and I have to revise the stop script part. Thanks for the resource too.
Title: Re: On the notion of Powershell
Post by: Fortuna on July 07, 2021, 07:03:26 PM
I've since had to write a new script that was significantly more complicated than the first one. The scenario was such that a company lost all of their databases due to a Godzilla attack. They only backed up 2 CSV files with employee and customer information, and I had to create a new Active Directory OU and a SQL server database and rebuild both with user information. A zillion lines of red text later, I just sent in the script to be graded. I also bought a couple Powershell books, one on the workings of the console and cmdlets, and one on scripting. As frustrating as the last script was to write, I'm starting to really like it and I'm going to start from the top with everything on my own time.

And it passed. The annoying thing about this class is that your script has to be 100% correct or else you don't pass. Each time you get something wrong you have to discuss the problem with the professor. No partial grades or anything.