can't download sample program

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

can't download sample program

Post by mqh77777 »

From this site I'm trying to download the timer example script. I have Deleted all Cookies from with Chrome and IE 11. Yet from both browsers when I try to access the Downloads page I get this error.


Disallowed Key Characters.

If you are seeing this message, please delete the cookie "__utmt_~1" and try again.


Is this downloads link working?
User avatar
J A Reif
Posts: 241
Last visit: Fri Mar 22, 2024 8:12 am
Answers: 1
Has voted: 1 time
Been upvoted: 1 time

Re: can't download sample program

Post by J A Reif »

For your convenience, I have uploaded the file here. We have modified our site so that you should no longer receive the error message. However, our site is hosted in the cloud and it may take a bit of time for all nodes to be updated with the new code. Our apologies for the inconvenience.
Attachments
PassingAndReturningValuesSampleProject.zip
(7.07 KiB) Downloaded 197 times
June Alane Reif
SAPIEN Technologies, Inc.
User avatar
J A Reif
Posts: 241
Last visit: Fri Mar 22, 2024 8:12 am
Answers: 1
Has voted: 1 time
Been upvoted: 1 time

Re: can't download sample program

Post by J A Reif »

My apologies. I posted the wrong sample. Here is the timer control sample script.
Attachments
TimerControl.zip
(79.68 KiB) Downloaded 209 times
June Alane Reif
SAPIEN Technologies, Inc.
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Re: can't download sample program

Post by mar10c-- »

The downloads (.zip)now work. Don't see the errors anymore. Thanks for the fast response.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: can't download sample program

Post by mqh77777 »

Thank you. That time control script works as expected but when I try to merge that logic with my existing code it does not work. What is interesting is the following code complies and runs without error, but I never see my countdown. Any hints of why/where my code is wrong?


$FormEvent_Load = {
#TODO: Initialize Form Controls here
$TotalTime = 10
$labelTime.Text = "{0:D2}" -f $TotalTime
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
$timerUpdate.Start()
}


$buttonDelayRebootFor1Hour_Click = {
#TODO: Place custom script here
$regkeypath = "hklm:\Software\MGH"
$value1 = (Get-ItemProperty $regkeypath).OneHour
If ($value1 -contains '1') { Start-Sleep -m 1 }
ELSE { New-ItemProperty HKLM:\SOFTWARE\MGH -name "OneHour" -Value "1" }
$MainForm.Close()
}


$buttonRebootNow_Click = {
#TODO: Place custom script here
remove-itemproperty -path HKLM:\Software\MGH -name OneHour
Restart-Computer localhost -Force
$MainForm.Close()

}


$timerUpdate_Tick = {
#Use Get-Date for Time Accuracy
[TimeSpan]$span = $script:StartTime - (Get-Date)

#Update the display
$formSampleTimer.Text = $labelTime.Text = "{0:N0}" -f $span.TotalSeconds

if ($span.TotalSeconds -le 0)
{
$MainForm.Close()
}

}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: can't download sample program

Post by jvierra »

You cannot use Start-Sleep in a GUI.

Look closely at the example and read the blog articles on how to use the controls.

YOU have to start simple and learn how to add a timer to a form so it can get events. You cannot just copy and paste code. It has to be hooked up to the controls on the form.


1. place timer on form.
2. set time values
3. create event for the tick by clicking on the time.
4. place code to update screen value with time value on each tick.

Once you can do that you will understand how forms and timers work.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: can't download sample program

Post by mqh77777 »

Question, why can't you put a start-sleep in a form? It seems to work fine. In fact within another form I have this line of code:

if ($GetBootValue1 = '1') { remove-itemproperty -path HKLM:\Software\MGH -name OneHour | start-sleep -s 3600 | shutdown.exe /r /f /t 180 /c "Your computer will be rebooted in 3 minutes as part our MGH computer maintenance process. Please close all open program." }

and it works perfect. It waits and then runs the shutdown.exe
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: can't download sample program

Post by jvierra »

Sleep block all form events while you are sleeping. It is just a very bad design choice. If you have to wait use a job or a timer. Sleep is for console apps.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: can't download sample program

Post by jvierra »

I cannot understand why you wanted the sample program when you are just running a simple two line program. You cannot have the user click on anything while the form is "sleeping"
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: can't download sample program

Post by mqh77777 »

I have 2 programs.

Program One: has no dialogs. It checks to see if your machine has been up for 7 days. If you have not rebooted in 7 days or more Program Two gets called. Program One will get launched either via Scheduled Task or at Logon Via Group Policies.


Program Two: This has 2 buttons. Delay Reboot for 1 hour & Reboot Now. If this pops up and the user never chooses either button I need the countdown to run for 5 min and then Auto-Reboot the system.



CODE FOR PROGRAM ONE:

function Get-Uptime
{
$o = Get-WmiObject -Class win32_operatingsystem
(Get-Date).date - $o.ConvertToDateTime($o.LastBootUpTime)
}


if ((Get-Uptime).TotalDays -lt 7)
{
cmd.exe /c "\\ServerName\Scripts\Tools\MGHRebootTool\MGHRebootTool.exe"
Start-Sleep -s 10
$GetBootValue1 = (get-ItemProperty hklm:\SOFTWARE\MGH).OneHour
if ($GetBootValue1 = '1') { remove-itemproperty -path HKLM:\Software\MGH -name OneHour | start-sleep -s 3600 | shutdown.exe /r /f /t 180 /c "Your computer will be rebooted in 3 minutes as part our MGH computer maintenance process. Please close all open program." }
}
}


--------------------------------------------------------------

CODE FOR PROGRAM TWO

$OnLoadFormEvent={
#TODO: Initialize Form Controls here

}

$buttonDelayRebootFor1Hour_Click={
#TODO: Place custom script here
$regkeypath = "hklm:\Software\MGH"
$value1 = (Get-ItemProperty $regkeypath).OneHour
If ($value1 -contains '1') { Start-Sleep -m 1 }
ELSE { New-ItemProperty HKLM:\SOFTWARE\MGH -name "OneHour" -Value "1" }
$MainForm.Close()
}


$buttonRebootNow_Click={
#TODO: Place custom script here
remove-itemproperty -path HKLM:\Software\MGH -name OneHour
Restart-Computer localhost -Force
$MainForm.Close()

}


So it is within Program Two that I need the time countdown. And I'm not sure how to add the code yet but that is where I'm at.

I hope this clears things up and sorry for not being clear off the bat.
This topic is 9 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked