Hi.
I am trying to create a Powershell Form application for fun. It seems like most things are working as they should, but I am struggeling to find out how I can set a time-limit for my function to run. The main goal is to let the user enter the desired number of minutes a ping should run against a target computer. The pingInfo function should then run as long as the user have entered. Beeing a Powershell n00b I am still trying to learn some magic:) The next objective would be to have this little application write the info from the console to file so I can have all log-files that are mentioned in this script. The app contain a couple more files that I can post if it would help (global, and startup). Can anyone tell me where I have screwed things up? The application keeps pinging forever as it is now, so I would be happy if someone could help me out here. Thanks! :)
$OnLoadFormEvent={
#TODO: Initialize Form Controls here
$Form=$MainForm
$Form.Controls.Add($textboxFind)
$Form.Controls.Add($textboxtime)
$Form.Controls.Add($logfoldernametext)
$Form.Controls.Add($cmdoutput)
$Form.Controls.Add($exitbutton)
$Form.Controls.Add($buttonBrowseFolder)
$Form.Controls.Add($logfolderapply)
$Form.Controls.Add($viewlogsbutton)
$Form.Controls.Add($logallno)
$Form.Controls.Add($logallyes)
$Form.Controls.Add($startpingbutton)
$Form.Controls.Add($stopping)
}
#Initializing Functions
# FUNCTION: Show an Open Folder Dialog and return the directory selected by the user.
function BrowseFolder()
{
$logfoldername=$parameters['A']
explorer.exe $logfoldername
}
function pingInfo {
$target=$textboxFind.text #we're taking the text from the input box into the variable $serverinfo
$pingtime=$textboxtime.text
if ((($target).length) -lt "1") {
Write-Output "No Target IP Specified."
Write-Output "EXAMPLE: Check-Pings.ps1 www.google.com"
break
}
$didnt = 0
$worked = 0
$datetime = (get-date)
Write-Host "`nLoading..." -nonewline -foregroundcolor yellow
$ping = new-object System.Net.NetworkInformation.Ping
Write-Host "`rStarted ping to $target $datetime`n"
while (1) {
$go = $ping.send("$target")
$result = $go.status
$ms = $go.roundtriptime
$ip = ($ping.send("$target").address).ipaddresstostring | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
if ($result -match "Success") {
$time = (get-date)
$worked = ($worked + 1)
if ($didnt -gt "0") {
$failrate = ( ($didnt / $worked ) * 100).tostring(".0")
Write-Output `r"Ping Successful ($ip) - $time - $failrate% Loss - $ms ms " | Out-File "C:\$LogFoldername\Success_With_Failrate.log"
}
else {
Write-Output `r"Ping Successful ($ip) - $time - 0% Loss - $ms ms " | Out-File "C:\$LogFoldername\Success.log"
}
}
Else {
$time = (get-date)
$didnt = ($didnt + 1)
if ($worked -gt "0") {
$failrate = ( ($didnt / $worked ) * 100).tostring(".0")
$errormessage = "Ping Failed - $ip - $time - $failrate% Loss ($didnt lost) "
Write-Output " "
$errormessage | Out-File "C:\$LogFoldername\Ping_Failed.log"
}
else {
$didnt = ($didnt - 1)
Write-Output `r"$ip - $time - Host not sucessfully pinged yet. " | Out-File "C:\$LogFoldername\Not_Successfull_Yet.log"
}
}
sleep -milliseconds 500
}
}#End of Ping Function
#End of function initializing
$exitbutton_Click={
#TODO: Place custom script here
[environment]::exit(0)
exit
}
$cmdoutput_TextChanged={
#TODO: Place custom script here
$cmdoutput.Text=pingInfo
}
$startpingbutton_Click={
#TODO: Place custom script here
pingInfo
#TODO: Find some cool way to let the function run for a limited time. This time-limit should be taken from the variable $pingtime
}
$viewlogsbutton_Click={
BrowseFolder
}
$logfolderapply_Click={
#TODO: Place custom script here
$logfoldername=$logfoldernametext.text
$parameters['A'] =$logfoldernametext.Text
$parameters | Export-Clixml -Path "C:\$Foldername\$ConfigName"
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“You have changed the default location for PingChekcer logs. Logs will now be saved in $logfoldername.”,
“Logs will be saved in new location!”, [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
}
$logfoldernametext_TextChanged={
#TODO: Place custom script here
}
$buttonBrowseFolder_Click={
if($folderbrowserdialog.ShowDialog() -eq 'OK')
{
$logfoldernametext.Text = $folderbrowserdialog.SelectedPath
}
}
$stopping_Click={
#TODO: Place custom script here
Exit
}
Best Regards Thomas Mortsell