Help with a Timer control issue I'm having
Posted: Fri Feb 12, 2016 5:38 am
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
}
}
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
}
}