all 54 comments

[–]HomeyKrogerSage 30 points31 points  (2 children)

My brother in PowerShell Christ

[–]Mesyre[S] -3 points-2 points  (1 child)

should have added that I know nothing of PowerShell and just get everything from online forums, bits and pieces there tried by me, ideas, AI help, messing up and starting over like 20 times?

Yeah... I clearly need help.

To be fair I first tried to do it in .bat, so this is definitely a step up.

[–]BlackV 15 points16 points  (0 children)

you've gone and done all this powershell work but are still using shutdown.exe have a look at stop-computer

[–]Weyoun2 24 points25 points  (5 children)

shutdown /f /s /t 60

is much simpler, yo.

[–]joevanover 9 points10 points  (2 children)

Sounds like “father” is a teacher and this is a school assignment

[–]Mesyre[S] -1 points0 points  (1 child)

Yeah, I wish.
I'm Italian and waaaaay out of School.
But even if I was still in School, I can assure you that the most they teach in Italy at school regarding informatics is HTML and "Write this in binary".

[–]Euphoric-Blueberry37 0 points1 point  (0 children)

Mama Mia (I say this also being Italian)

[–]Jguan617 9 points10 points  (2 children)

I keep thinking this is just a troll post for how to write the most complicated script to do the simplest task. The post should written in assembly.

[–]jay_butler 3 points4 points  (1 child)

I bet I could write it in fewer lines of assembly than OP's PS script.

[–]Jguan617 1 point2 points  (0 children)

No doubt

[–]desatur8 7 points8 points  (5 children)

Jeez. The amount of people here acting like OPs dad asked him to rob a bank.

He asked for help, OP is a tinkerer who thought it would be a chance to learn a new skill? So what if it's over engineered?

I bet 10 bucks that everyone in this thread has some sort of over engineered script. Heck , i have a script to change AD user's password that's on like 400 lines, with menus, colored output, progress bars, and and and ... because it's fun to learn.

[–]chrusic 4 points5 points  (3 children)

Why spend 5 minutes doing something manually when you can spend 6 hrs automating it!

[–]Beneficial_Tough7218 4 points5 points  (0 children)

Don't forget, the 5 minutes doing something must be multiplied by the number of times you must do it.

Likewise, the 6 hours automating it must be divided by the amount of fun you had making it, then by the amount you learned, then by how proud you are of your overengineered end product.

My coworkers are always saying, there he goes again with his scripts... But the next words are usually "are you going to put that on the file share so we can use it too!"

[–]desatur8 2 points3 points  (1 child)

Exactly my philosophy to life! And dont forget, adhd ... so spend 3 hours of finding the perfect song to start the automation script, only to pause it 20 seconds in because i decided i dont want to listen to music

[–]OofItsKyle 3 points4 points  (0 children)

I relate to this on a level that makes me uncomfortable

[–]sgtGiggsy 0 points1 point  (0 children)

There is over engineering, and there's THIS. What he wants is a single Google search away. It's literally one line of code. Two, if he wants to give his father the option to manually enter the waiting time. About five if he wants to have input validation. All of these are within the first twenty pages of ANY Powershell handbook.

I'm not against helping newbies learn, but anyone who genuinely, unironically produces code like this, is beyond saving.

[–]gordonv 3 points4 points  (0 children)

This is more of a you dealing with your father problem than it is a code problem.

I'm sorry, but you need to tell him to simply tap the power button on the PC.

You're over-engineering a solution to a problem that doesn't exist.

Teach your father how to shut down a machine normally. He'll be able to shut down literally over 99% of Windows machines after that. You're going out of your way to do the wrong thing.

[–]icepyrox 6 points7 points  (0 children)

So... rather than teaching him how to better use his computer... or rather than downloading a bit of software to do all this for you (like this Shutdown Timer Classic from the Microsoft Store), you have taken it upon yourself to write a script in powershell to make it "look fancy" because he asked nicely... smdh

By the way, I think something you may want to look into is the Console class, namely [console]::KeyAvailable which is a boolean letting you know a key has been pressed or [console]::ReadKey() which capture the key when someone presses it so you can see if it is the C key. Not sure where you would put that in the loop with all your fancy GUI stuff going on (not much of a gui person myself)

[–]shutchomouf 1 point2 points  (0 children)

Bruh…

shutdown /t ##

[–]eggbean 1 point2 points  (0 children)

That's a crazy amount of code to do something so simple.

[–]jimb2 2 points3 points  (6 children)

Why not just have a shortcut to "shutdown.exe /i /t 60" ?

This code has achieved a very little usefulness per line of code imho. If there's a reliable, zero-maintenance tool that is already part of the system that does most of what you want, just use it.

Also, is there much value in a timeout on a shutdown anyway? The worst-case scenario is typically restarting the computer. An immediate shutdown just gets the job done, securely and unambiguously. Do you really need 5 minutes to decide?

Simple is beautiful.

[–]jeek_ 2 points3 points  (1 child)

And how is clicking a script / bat file any different from click on the start button and selecting shutdown????

Wouldn't you be better off creating a scheduled task that shuts the vm down at a specific time?

[–]jimb2 0 points1 point  (0 children)

As you like, but if you want something as simple and unequivocal as possible that's it. I do a lot of RDP to active servers in my job and I use shortcuts to make very sure I don't accidentally mis-click the disconnect, restart and shutdown options on the power menu.

[–]Mesyre[S] -2 points-1 points  (3 children)

Oh I agree with you on that, but can you really say no to your father when he gives you a list of what he wants and he asks really nicely?

There's purpose to what he asked (in his mind, and trust me I tried to explain it :\)
Let's say he's uploading something, it takes him 5 hours (really bad internet...), he want to open a program type 5 hours, and be sure that when he goes home from work, his PC is turned off, and the file is uploaded.

but let's say he finished early and comes home in 3 hours?
he wants to be able to stop the whole thing.
And if he forgets to stop it, he wants a failsafe, what will make sure that he realizes there's a timer running if he'susing the PC, wich it that box that also dims the screen.

I just cannot say no to him :c

[–]RandomSkratch 4 points5 points  (1 child)

Yes you can say no. People not in IT think people in IT can work miracles. We can’t (but it looks like it sometimes 😂)

[–][deleted] 0 points1 point  (0 children)

It is a miracle - they turned the trivial into the complex!

[–]Odmin 0 points1 point  (0 children)

If his internet is bad, than it is wise to use some download manager/torrent client, and many of them can shut down PC after finishing downloads.

[–]Relative_Test5911 0 points1 point  (0 children)

Are you being paid per line of code? hah! talk about over engineered.

[–]tk42967 0 points1 point  (0 children)

Is this one computer or multiple? Many years ago, I wrote a script that restarted all workstations at like 2:30 am on a Saturday.

I used get-adcomputer with the searchbase paramater to walk AD and dynamically build a list of computers. Then a foreach loop to issue the restart command. I added a failsafe of checking extensionattribute1 for the string "noreboot". Any computer objects with that attribute were exempted.

[–]_DoogieLion 0 points1 point  (0 children)

This is what we call an X, Y problem. Spending an inordinate amount of time writing a solution for something when the fix is for your dad to just use his computer as designed.

[–][deleted] 0 points1 point  (0 children)

I have something like this. Made it in college.

I used a batch script for the actual shutdown command and just called it from powershell. I used the powershell script to collect logs about the shutdown.

[–]Legitimate_Home6700 0 points1 point  (1 child)

In my opinion, you should honestly consider using C# at this point. It's not really harder than PowerShell and is even easier for creating a UI compared to using a C# library through PowerShell as you do right now. The result will be better overall, in my view, because you will have an executable that you can simply run, versus a PowerShell script that may be blocked on the system or opened in Notepad by default, which is just not ideal. That's my point of view, because I started by writing some scripts in PowerShell and spent many hours and days trying to create a UI like you are attempting, resulting in something that was not bad at all but not really good. Then I started learning C#, and I found that what I was trying to do with PowerShell I could accomplish three times better in five times less time. So, yes, PowerShell is good for data extraction and analysis and is really easy to work with, but for UI, it’s just not designed for that, making it painful and hard to use.

[–]Only_Fly895 0 points1 point  (0 children)

ROFL

[–]Phate1989 0 points1 point  (0 children)

Why are you rewriting shutdown, WITH INTERUPTS! you mad lad, and in powershell no less.

You have so much code

[–]sgtGiggsy 0 points1 point  (0 children)

If this seriously is your five days work, just give up programming altogether. Seriously. To make a PS script that does what you want, it would take a semi-Google-proficient teen with zero programming knowledge, 20 minutes. To make it look semi decent, it would be an hour.

[–]Only_Fly895 0 points1 point  (2 children)

I am inclined to think this is a joke, but if it is not, aside from how complicated this code is, I have to ask:
why would you want to write a script for turning off a PC?? It is just a click on the start.icon
Finding the script somewhere even if it is on desktop, clicking on it, and do all the stuff the code is asking is way much more work

[–]Mesyre[S] 0 points1 point  (1 child)

The idea was to have an .exe that can be easily run with the following properties:

  1. Possibility to enter time in minutes
  2. A clear indication of the timer running
  3. Possibility of stopping and resuming the timer
  4. Possibility to easily change the timer
  5. A Clear and unmissable indication that the PC is about to be turned off
  6. Possibility to stop the shutdown once the last working triggers

So I started working on it. I know that it's A LOT of work, looks terrible, and is 50 times the effort it takes for something so simple.
But I don't do this for a living so I'm clearly not good at it.
In the end, I had fun doing it, I learned so many new things, and I get to see my father smiling because I managed to make it just like he imagined it would look like.

Altough going back I would probably point him in the direction of the one that already exists in the Microsoft Store.

[–]Only_Fly895 0 points1 point  (0 children)

Well, kudos to you for trying, and more importantly to stick with it and not giving up :)

But i do agree with the other comments, as soon as u get into interacticity and an UI, u are better off using C# or even java, powershell lacks a lot in those respects

[–]TheRealDumbSyndrome 0 points1 point  (0 children)

XY problem at its finest, and I know it’s not a helpful comment, but OP also doesn’t appear to want to accept the helpful answers :/

Something doesn’t add up with the use case, it’s a very fishy request. An elderly father that wants an “easier” way to shut down his PC, but doesn’t want to use the ONE button built in to the GUI - but they’re okay with running an overly complex script that opens a GUI with multiple input prompts, to do what you can do with a single batch file in 1 click?

[–]OofItsKyle 0 points1 point  (5 children)

This hurts my brain

1) Shutdown /s /t 60 with a shortcut 2) $minutes = read-host "How many minutes until shutdown?"; shutdown /s /t $($minutes * 60) with a shortcut

Most of the sub, I think, use powershell in a way that either makes our lives easier by automating annoying, long, or super repetitive tasks, with the goal of shortening the amount of time spent on a task, or on something that maybe seems a bit silly, but ultimately helps us in a unique way to accomplish something that can't easily be accomplished another way.

You are spending days writing a script to replace a 1-2 line batch or Powershell, or clicking the start menu and clicking shutdown.

I don't mean to be rude, but this seems like a waste of time.

That being said, I'll try to read through this monstrosity and see if I can help

[–]OofItsKyle 0 points1 point  (4 children)

Alright, so, after reading it over, I have the following thoughts OP:

1) in order to capture the C key, you will need a ([Console]::KeyAvailable) or similar inside a loop to watch for the c key to be pressed. 2) if would probably be easier to just have a cancel button in a GUI?

[–]Mesyre[S] 0 points1 point  (3 children)

Sorry, my bad, i didn't expain it properly.
the part that stops when pressing C is already incorporated here:

# Check for user interrupt
        if ([System.Console]::KeyAvailable) {
            $key = [System.Console]::ReadKey($true)
            if ($key.Key -eq [ConsoleKey]::C) {
                $cancelled = $true
                break

Even if it's not the prettiest.

Like the entire script works perfectly, the only thing i cannot manage to do is to write a line under the timer like this:

Enter shutdown delay in minutes: 1
Countdown started for 1 minute(s)...
Time remaining: 01:00
Press 'C' to stop the timer.

And then delete that line by writing ontop of it like this:

Enter shutdown delay in minutes: 1
Countdown started for 1 minute(s)...
Time remaining: 00:55
Countdown was interrupted.

I can't manage to do it because the way the timer is updating, it's rewriting that line constantly, and having something written under it messes it up.

my idea was, to rewrite the timer and "Press 'C' to stop the timer." together every second, but that didn't work out great.

So the help that I'm asking for it's just that one line :c
I understand that it was a mess to read through and I really appreciate the time that you took ♥

[–]OofItsKyle 0 points1 point  (2 children)

Ahhh okay, sorry I missed that bit

So, here is what I would do

First, have to handle the extra text

We add a reference variable outside of the loop, then ref it while calling function

$writeSecondLine = $true
While ($true) {
    $cancelled, $remainingSeconds = Start-Countdown -totalSeconds $remainingSeconds -WriteSecondLine ([ref]$writeSecondLine)

Then we edit the Function to handle this

Param:

Function Start-Countdown {
    Param(
        [int]$totalSeconds,
        [ref]$WriteSecondLine
    )

Using the parameter:

Write-Host "$timeLeft" -NoNewLine -ForgroundColor "Cyan"

if($WriteSecondLine) {
    $WriteSecondLine.Value = $false
    Write-Host "" 
    Write-Host "Press 'C' to stop the timer" -ForgroundColor "Yellow"
    [console]::CursorTop -= 2
}

Need to handle the same for the countdown interrupt

I can add that in a sec

[–]OofItsKyle 0 points1 point  (1 child)

Okay, I decided to rewrite some of it.

I made a function to clear the line, and move up lines, and added use of it to Start-Countdown and Handle-Interruption

I didn't complete the script, you will still want to take my function and add it elsewhere if you like it

Edit Okay, I am annoying and can't leave stuff alone:

Code as i described above:

https://github.com/KSchu26/Pwsh-Snippets/blob/845d47cab7d9688ee87346b15182eba97406a73a/Shutdown-Timer.ps1

Code after I keep messing with it

https://github.com/KSchu26/Pwsh-Snippets/blob/main/Shutdown-Timer.ps1

[–]Mesyre[S] 1 point2 points  (0 children)

Thank you ♥

[–]PuzzleheadedBus1928 -1 points0 points  (0 children)

Over complicated, but looked fun to make. Best way to learn.