Hi all,
I’m creating background jobs to perform a task.
I get certain values from a DataGridView
I’m adding the results in to another DGV and removing them from the source DGV.
I’m controlling the flow to limit the amount of background jobs running at one time.
everything works as expected but in the source i’m left with some items still in the source DGV after the loop that goes through each item in the source DGV.
The code for the button that executes the command is below:
$ScanButton_Click = {
$ScanButton.Enabled = $false
foreach ($a in $ItemsToScanDGV.Rows)
{
$AddJobs = $true
while ($AddJobs -eq $true)
{
if ($JobTrackerList.Count -ge ($concurrentjobstb.Text -as [int]))
{
$AddJobs = $true
[System.Windows.Forms.Application]::DoEvents()
}
else {
$ComputerName = (($a.Cells[$ItemsToScanDGV.Columns["HostName"].Index]).Value)
$ComputerName = $ComputerName.ToString().Trim()
if ($ComputerName -gt "")
{
$SourceCellIndex = $a.Cells[$ItemsToScanDGV.Columns["HostName"].Index]
Add-JobTracker -Name ("$ComputerName" + (Get-Random -Minimum 1000 -Maximum 9999)) -ArgumentList $ComputerName -JobScript {
param ($Computer)
$ObjectProps = @{
"HostName" = "";
"IPAddress" = "";
"LastRestarted" = "Could Not Query";
"RestartedRecently" = "N/A";
"RDP" = "N/A";
"TryAgain" = "TryAgain"
}
$NetworkTest = Test-NetConnection -CommonTCPPort RDP -ComputerName $Computer -ErrorAction SilentlyContinue
if ($NetworkTest.PingSucceeded)
{
try
{
$WMI = Get-WmiObject -Class win32_operatingsystem -ComputerName $Computer -ErrorAction 'Stop'
if ($WMI.LastBootUpTime -lt (Get-Date).AddHours(-1)) { $LastHour = "Yes" }
Else { $LastHour = "No" }
$obj = New-Object System.Management.Automation.PSObject -Property $ObjectProps
$obj.HostName = $Computer
$obj.IPAddress = $NetworkTest.RemoteAddress
$obj.LastRestarted = $WMI.ConvertToDateTime($WMI.LastBootUpTime)
$obj.RestartedRecently = $LastHour
$obj.RDP = $NetworkTest.TCPTestSucceeded
Write-Output $obj
}
catch
{
$obj = New-Object System.Management.Automation.PSObject -Property $ObjectProps
$obj.HostName = $Computer
$obj.IPAddress = $NetworkTest.RemoteAddress
Write-Output $obj
}
}
Else
{
$obj = New-Object System.Management.Automation.PSObject -Property $ObjectProps
$obj.HostName = $Computer
$obj.IPAddress = $NetworkTest.RemoteAddress
Write-Output $obj
}
} -CompletedScript {
param ($job)
$hashReturn = Receive-Job -Job $Job
ForEach ($Job in $hashReturn)
{
$OutputDGV.Rows.Add($Job.HostName, $Job.IPAddress, $Job.LastRestarted, $Job.RestartedRecently, $Job.RDP, $Job.TryAgain)
foreach ($row in $ItemsToScanDGV.Rows)
{
if ($row.Cells["HostName"].Value -ne $null)
{
if ($row.Cells["HostName"].Value.ToString().Equals($Job.HostName))
{
$ItemsToScanDGV.Rows.Remove($Row)
break
}
}
}
}
If ($JobTrackerList.Count -le 1) { $ScanButton.Enabled = $true }
}
}
$AddJobs = $false
[System.Windows.Forms.Application]::DoEvents()
}
}
}
}
Anyone know what i’m going wrong?
I’m happy to upload the project files somewhere is needed.
Edit: in fact, here’s the link anyway Dropbox - File Deleted