Page 1 of 1

Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 5:38 am
by ojrivera381
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
}
}

Re: Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 6:02 am
by ojrivera381
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

Posted: Fri Feb 12, 2016 6:53 am
by jvierra
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.

Re: Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 7:38 am
by ojrivera381
Thanks for the reply. The properties are set to false.
Image

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

Posted: Fri Feb 12, 2016 7:43 am
by jvierra
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?

Re: Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 7:43 am
by jvierra
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?

Re: Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 10:47 am
by ojrivera381
Thanks for the help. I've gone through all the code I don't see it anywhere else.

Re: Help with a Timer control issue I'm having

Posted: Fri Feb 12, 2016 10:58 am
by jvierra
Well you may have broken code in your project. Try starting a new project and adding the code a little bit at a time.