Hi.
I have a small powershell application that runs a background job. This is used for learning more about powershell, so please dont laugh to much:) This application is built in Powershell Studio as a Forms application running powershell. I have a function;"PingInfo" that collects some data. When I'm running this as a background job I dont get any output to the application console or to the log-files it is creating. If I dont run the function as background job everything works well. Can anyone give me a hint about how to stream live data from the backgroun job(function) to a textbox and logfile while the job is running? I want to do it this way so I keep the application window open (it is preventing input if I dont run the job in backgroud). If it is any need I can post the project-files with screens and so on. This is the "non-working" script. Non-working means it does not output any data to the application window of log-files.
$OnLoadFormEvent={
$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)
}
$job1 = {pingInfo}
#Initializing Functions
# FUNCTION: Write output message when ping is completed.
function writecomplete {
Write-Output "`rPing Completed" | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
}
# FUNCTION: Show an Open Folder Dialog and return the directory selected by the user.
function BrowseFolder {
$parameters = Import-Clixml -Path "C:\$Foldername\$ConfigName"
$logfoldername=$parameters['B']
explorer.exe "C:\$logfoldername"
}
# FUNCTION: Function to ping the selected target.
function pingInfo {
$target=$textboxFind.text #we're taking the text from the input box into the variable $serverinfo
$Duration=$textboxtime.text
if ((($target).length) -lt "1") {
Write-Output "No Target IP Specified." | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output "EXAMPLE: Check-Pings.ps1 www.google.com" | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
}
$didnt = 0
$worked = 0
$datetime = (get-date)
[datetime]$start_time=(Get-Date)
$finish_time=$start_time.AddSeconds($Duration)
Write-Output "`nLoading..." | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
$ping = new-object System.Net.NetworkInformation.Ping
Write-Output "`rStarted ping to $target $datetime`n" | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output "`rStarted ping to $target $datetime`n" | Out-File -Append "C:\$LogFoldername\$target.Pingchecker.log"
While((Get-date) -lt $finish_time) {
$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 $target - $time - $failrate% Loss - $ms ms " | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output `r"Ping Successful $target - $time - $failrate% Loss - $ms ms " | Out-File -Append "C:\$LogFoldername\$target.Pingchecker.log"
}
else {
Write-Output `r"Ping Successful $target - $time - 0% Loss - $ms ms " | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output `r"Ping Successful $target - $time - 0% Loss - $ms ms " | Out-File -Append "C:\$LogFoldername\$target.Pingchecker.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-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output " " | Out-File -Append "C:\$LogFoldername\$target.Pingchecker.log"
}
else {
$didnt = ($didnt - 1)
Write-Output `r"$ip - $time - Host not sucessfully pinged yet. " | Out-String -Stream | foreach-object {
$cmdoutput.lines = $cmdoutput.lines + $_
$cmdoutput.Select($cmdoutput.Text.Length, 0)
$cmdoutput.ScrollToCaret()
$MainForm.Update()}
Write-Output `r"$ip - $time - Host not sucessfully pinged yet. " | Out-File -Append "C:\$LogFoldername\$target.Pingchecker.log"
}
}
sleep -milliseconds 500
}
writecomplete
}#End of Ping Function
#End of function initializing
$cmdoutput_TextChanged={
#TODO: Place custom script here
}
$startpingbutton_Click={
#TODO: Place custom script here
Start-Job $job1
}
$viewlogsbutton_Click={
BrowseFolder
}
$logfolderapply_Click={
#TODO: Place custom script here
$logfoldername=$logfoldernametext.text
$parameters['B'] =$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
$target=$textboxFind.text #we're taking the text from the input box into the variable $serverinfo
$Duration=0
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“Pinging of $target have been cancelled or terminated.”,
“Ping-job terminated!”, [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
$cmdoutput.Text=""
$textboxtime.Text=""
$textboxFind.Text=""
}
$labelexit_Click={
#TODO: Place custom script here
[environment]::exit(0)
}
Best Regards Thomas Mortsell