Hi all
I have created a Powershell gui script for mapping a drive for a client with a timer to disconnect after 1 hour. Using the timer control sample in the tutorial.
The issue I'm having is the timer is starting during form load and not when the button is clicked. What happens is if the the button is clicked after the timer expires then script process immediately to the end Any help with this would greatly be appreciated. I have searched and been beating my head on this for days now.
$OnLoadFormEvent={
#TODO: Initialize Form Controls here
$ndrive = test-path N:
If ($ndrive -eq $true) { cmd /c "net use N: /delete" }
else { "Statement is False" }
$TotalTime = 30 #in seconds Set to 3600 for 1 hour timer. Testing with 30
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Write-Host "Timer disabled"
}
$buttonReMapNDrive_Click={
#TODO: Place custom script here
$buttonReMapNDrive.Enabled = $false
New-PSDrive -name "N" -PSProvider Filesystem -root <#"\\[enter server\Share here without brackets]"#> -Persist -Credential "" -Scope Global
$timerUpdate.Start()
Write-Host "Timer Enabled" #Comment to see the steps that occur will be cleaned up
}
$buttonDisconnectNDrive_Click={
#TODO: Place custom script here
Remove-PSDrive -Name N -force
Write-Host "Removed" #Comment to see the steps that occur Will be cleaned up
}
$timerUpdate_Tick={
#TODO: Place custom script here
[TimeSpan]$span = $script:StartTime - (Get-Date)
if ($span.TotalSeconds -le 0)
{
$timerUpdate.Stop()
Remove-PSDrive -Name N -force
Write-Host "SCRIPT ENDED" #Comment to see steps that occur will be cleaned up
$MainForm.close()
return
}
}
Help with a Timer control issue I'm having
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.
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.
- ojrivera381
- Posts: 4
- Last visit: Fri Apr 01, 2022 3:15 am
- ojrivera381
- Posts: 4
- Last visit: Fri Apr 01, 2022 3:15 am
Re: Help with a Timer control issue I'm having
My apologies I think I posted this in the wrong section looks like it should be in Powershell Studio
Re: Help with a Timer control issue I'm having
No - you are in the correct place.
Just set the properties of the timer to enabled =$false and the timer won't start.
I suggest using a timer control and keeping a list of drives and time and just test the time.
Just set the properties of the timer to enabled =$false and the timer won't start.
I suggest using a timer control and keeping a list of drives and time and just test the time.
- ojrivera381
- Posts: 4
- Last visit: Fri Apr 01, 2022 3:15 am
Re: Help with a Timer control issue I'm having
Thanks for the reply. The properties are set to false.
Am I supposed to add it to the code? If so where. Ps I'm new to this IDE
Thanks again for the help
Am I supposed to add it to the code? If so where. Ps I'm new to this IDE
Thanks again for the help
Re: Help with a Timer control issue I'm having
The timer does not start until you tell it to. If it is running when the form opens then you have enabled it in code or in the designer.
You say the "properties" are set to false What properties are you referring to?
You say the "properties" are set to false What properties are you referring to?
Re: Help with a Timer control issue I'm having
The timer does not start until you tell it to. If it is running when the form opens then you have enabled it in code or in the designer.
You say the "properties" are set to false What properties are you referring to?
You say the "properties" are set to false What properties are you referring to?
- ojrivera381
- Posts: 4
- Last visit: Fri Apr 01, 2022 3:15 am
Re: Help with a Timer control issue I'm having
Thanks for the help. I've gone through all the code I don't see it anywhere else.