Page 2 of 3

Re: Help with the responsive form

Posted: Fri Sep 22, 2017 4:39 pm
by jvierra
If you are running multiple jobs then you will need to do it like this:

Code: Select all

$Test = $listview.Items.Add("Copying Folder")
if (Test-Path "C:\Temp\From") {
	
	$Test.Subitems.Add("Folder exists")
	$Test.Subitems.Add("Copying...")
	Add-JobTracker -Name "Test" `
				   -JobScript $sb -ArgumentList "C:\Temp\From", 'C:\test' `
				   -CompletedScript {
		Param ($job)
		$Compare = CompareFolderSize "C:\Temp\From" 'C:\test'
		if ($Compare -eq $true) {
			$job.ListViewItem.Subitems[2].Text = "Success"
		} else {
			$job.ListViewItem.Subitems[2].Text = "Error"
		}
		
	}` -UpdateScript {
		Param ($job)
		while (Get-job -State "Running" | Where-Object{ $_.Name.Contains("Test") }) {
			$job.ListViewItem.Subitems[2].Text = "Still Copying"
		}
		
	}
	$job = Get-Job Test
	$job | Add-Member -NoteProperty ListViewItem -Value $test
} else {
	$copyError = "ERROR - Check folder or file"
	$Test.Subitems.Add($copyError)
}

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 3:07 am
by gutihz
Thanks for the replies and suggestions. Unfortunately neither of them worked.
I added

Code: Select all

[void][System.Windows.Forms.MessageBox]::Show("update part")


and also in the CompletedScript part and nothing shows up. I wonder if there is something wrong in the function?

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 3:27 am
by jvierra
You haven't implemented the whole JobTracker. It requires some global collections and a timer event. It is nit just a single function. No time tick - no results.

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 7:13 am
by gutihz
Obviously I'm new to this. I read the blog again, looked at the examples given and couldn't figure it out.

As I mentioned I do have multiple jobs that I want to copy from and to. I have a foreach loop that I pass my scriptblock with my arguments. Basically the same code in my previous post. Do I make the listview or datagrid as you suggested as a global variable?

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 10:49 am
by jvierra
The ListView is already a variable in scope. All controls are always in-scope. What is in your timer code? Have you started the timer? Is this a PSF or is it a hand coded form?

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 12:33 pm
by gutihz
I didn't think I had to start the timer since it is triggered inside the "Add-JobTracker" function.

Code: Select all

#Start the Timer
		if (-not $timerJobTracker.Enabled)
		{
			$timerJobTracker.Start()
		}
mine is a psf. I attached the file.

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 12:59 pm
by jvierra
Use a datagrid.
Use a CSV to create load the DGV. Include the folder path in the CSV.

You loop can get all values from the CSV/DGV.

Before doing that create a new form and add Tracker. Test the code suggestions with one job until you understand how it works. Once you understand then move the lesson to your form an manage jobs in a loop.

Unfortunately you have chosen a set of design requirements that are well beyond your current skill set. You will have to slowly work through this. Build one small piece at a time until you understand how it works. This is the nature of all programming. No programmer comes with built-in knowledge of any technology. Programming is about teaching yourself how things work.

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 1:45 pm
by jvierra
Food for thought. Here is how to approach managing jobs. Onc you understand how this works you can experiment with changing controls or adding other complexities.

# define this at script level so we don't create hundreds of them
$fso = New-Object -ComObject Scripting.FileSystemObject

Code: Select all

# define this at script level so we don't create hundreds of them
$fso = New-Object -ComObject Scripting.FileSystemObject

$jobScript = {
	Param ($strSource,$strDest)
	Robocopy.exe $strSource $strDest /MIR /V /R:2 /W:5
	
}

$updateScript = {
	Param ($job)
	$job.DataGridRow.Cells['Status'].Value = $job.State
}

$completedScript = {
	Param ($job)
	
	if ($fso.GetFolder($path1).Size -eq $fso.GetFolder($path1).Size) {
		$job.DataGridRow.Cells['Status'].Value = "Success"
	} else {
		$job.DataGridRow.Cells['Status'].Value = "Error"
	}
}

$MainForm_Load={
	$copyJobs = Import-Csv CopyJobs.Csv
	$datagridview1.DataSource = [ArrayList]$copyJobs
}

$buttonRunJobs_Click= {
	foreach ($row in $datagridview1.SelectedRows){
		$Name = $row.Cells['Name'].Value
		if (Test-Path $row.Cells['SourcePath'].Value){
			
			$jobParams = @{
				Name = $row.Cells['Name'].Value
				JobScript = $sb
				UpdateScript = $updateScript
				CompletedScript = $completedScript
				ArgumentList = @($row.Cells['SourcePath'].Value, $row.Cells['TargetPath'].Value)
			}
			$job = Add-JobTracker @jobParams

			$job = Get-Job $row.Cells['Name'].Value
			$job | Add-Member -NoteProperty DataGridRow -Value $row
		}else{
			$row.Cells['Status'].Value = "ERROR - Check folder or file"
		}
	}
}

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 2:58 pm
by jvierra
I took another quick look. You have no timer event. You must have deleted it somehow.

Re: Help with the responsive form

Posted: Sat Sep 23, 2017 5:33 pm
by jvierra
See the following for a complete working example: http://tech-comments.blogspot.com/2017/ ... -form.html