How to look ahead for the next iteration in a loop?

Currently, I have a script that loops through id’s and performs some actions based on that value, however, these id’s represent batches, and sometimes you can have several batches that are still related and in that case, I want to do something else. I’m trying to figure out how, from within the loop, I can look ahead to see what the next id or all the other id’s are, before actually getting to the next iteration.

Any ideas?

Can you please provide an example of the data you are working with and expected result? Doing what you are asking may not necessarily be best handled in a loop, but it’s hard to provide guidance without data.

Ok, we have a UI feature, where the user fills out a project code, a mediaID and a review export id, the piece I am refering to is the the review export, which can have a one to many relationship with media, so the review export id field can be passed comma delimited.

I just want to be able to look ahead, and if the next id is related to the same media id as the last, go ahead and kick of a new export without running the RunCaseManagement function…

the following is the code block, for the ForEach loop, in regards to the review export

Foreach ($ReviewExport in $ExportList)
		{
			$QueryText = @"
SELECT MIN(nf.fkMediaId) AS MediaID, re.pkReviewExportId AS pkReviewExportID
FROM EXT.nuixFiles nf
INNER JOIN EXT.reviewExport re
ON nf.fkReviewExportId = re.pkReviewExportId
WHERE re.reviewExport = '$ReviewExport'
GROUP BY re.pkReviewExportId
"@

			# Retrieve MediaID for current $ReviewExport
			$Result = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText)
			$EvidSet = $Result.MediaID
			$ExportID = $Result.pkReviewExportID			
			$ServerInstance = $ProjConnString.ServerName
			$ProjectDBName = $ProjConnString.DatabaseName
			
			# Check for exportID already existing in EXT.nuixProd, if so stop and send notification
			$QueryText = "select ISNULL(COUNT(*), 0) as value from ext.nuixprod WHERE exportID = $ExportID"
			$ExportExist = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			Write-Verbose -Message "Checking if ExportID, $ExportID, is in EXT.nuixProd..."
			If ($ExportExist -ne "0")
			{
				$ExportExistResults = $ExportExistResults + "ExportID, $ExportID, Already exists in EXT.nuixProd."
				
				continue
			}
			Else
			{
				Write-Verbose -Message "ExportID, $ExportID, does not exist in EXT.nuixProd...Continuing with Legal Export"
			}

			Try
			{
				# Get search available license servers available in the pool, then assign first available if license tests as OK
				$QueryResult = Get-QueryOutput -ServerInstance $CentralServerInstace -DatabaseName $CentralDatabase -QueryText "SELECT ServerNames FROM   [ADMIN].[GetNuixLicenseServerList]($DomainCampusID, '$HostNuixVersion');"
				$NewResults = ($QueryResult.ServerNames).ToLower()
				
				# Need the LicenseServernames variable to be an ArrayList
				#   to leverage the Remove() and Insert() functions
				[System.Collections.ArrayList]$LicenseServernames = $NewResults -split ","

				# Move local machine name to the front of the ArrayList of machines to check
				# IF it is present in the list of LicenseServernames
				If ($LicenseServernames -contains ([System.Environment]::MachineName).ToLower())
				{
					$LicenseServernames.Remove([System.Environment]::MachineName.ToLower())
					$LicenseServernames.Insert(0, [System.Environment]::MachineName.ToLower())
				}
				$NUIXLicenseServername = Get-NuixLicenseStatus -ServerList $LicenseServernames -HostNuixVersion $HostNuixVersion -NuixConsoleApp $NuixConsoleApp -SmtpServer $SmtpServer -Verbose
				$LicenseInfo1 = $NUIXLicenseServername[0].ToString()
				If ($LicenseInfo1 -eq "Failed")
				{
					$LicenseResults = $LicenseResults + $NUIXLicenseServername[1].ToString()
					continue
				}
			}
			Catch
			{
				$LicenseResults = $LicenseResults + "THERE ARE NO NUIX LICENSE SERVERS AVAILABLE! Please contact Client Applications to get the Nuix License updated and made available in the pool...`n"
				continue
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data for legal exports does exist before creating the case
				$VerifyCaseDataLegalExports = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "LegalExportCaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose
				
				$Verify1 = $VerifyCaseDataLegalExports[0].ToString()
				$Verify2 = $VerifyCaseDataLegalExports[1].ToString()
				
				# Error Handling for Case Data - Legal Export Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseDataLegalExports
			
					# stopping operation on this specid and going to the next iteration
					continue				
				}				
				
				# Copy Case files to the local Case directory
				$MoveToLocal = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToLocal" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToLocal[0].ToString()
				$Verify2 = $MoveToLocal[1].ToString()

				# Error Handling for Case Data - Move To Local
				Write-Host "Move To Local: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToLocal				

					# stopping operation on this specid and going to the next iteration
					continue				
				}
			}

			# Run OCR IF enabled for project and supported by nuix version; Must happen before Legal Export
			if (($NuixVersion.propertyValue.ToLower() -ge "v5.2") -and ($OCRSetting.propertyValue.ToLower() -eq "nuix"))
			{	

				$ParamList = @{
				'PsScriptPath' = "$PsScriptPath\Functions";
				'ServerInstance' = ($ProjConnString.ServerName);
				'DatabaseName' = ($ProjConnString.DatabaseName);
				'EvidenceSet' = $EvidSet;
				'InstalledNuixVersion' = "$HostNuixVersion";
				'NuixApplication' = "$NuixConsoleApp";
				'SessionNum' = $SessionNum;
				'SettingCategory' = "8030B5D7-CEC9-439B-BACC-07298531A7E4";
				'NuixOperation' = 'OCR';
				'ExportSet' = $ReviewExport;
				'LogsDirectory' = $LogsDirectory; 
				'TempDirectory' = $TempDirectory;
				'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

				$AuditTotals = RunNuixOperation @ParamList -Verbose
				
				$AuditResultsOCR = $AuditResultsOCR + $AuditTotals				
				
			} #IF OCR Enabled	
			
			# Run Legal Export
			$ParamList = @{
			'PsScriptPath' = "$PsScriptPath\Functions";
			'ServerInstance' = ($ProjConnString.ServerName);
			'DatabaseName' = ($ProjConnString.DatabaseName);
			'EvidenceSet' = $EvidSet;
			'InstalledNuixVersion' = "$HostNuixVersion";
			'NuixApplication' = "$NuixConsoleApp";
			'SessionNum' = $SessionNum;
			'SettingCategory' = "BEC8DE37-EB11-4FDC-B6A6-057D53CCB45F";
			'NuixOperation' = 'LegalExport';
			'ExportSet' = $ReviewExport;
			'LogsDirectory' = $LogsDirectory; 
			'TempDirectory' = $TempDirectory;
			'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

			$AuditTotals = RunNuixOperation @ParamList -Verbose

			$AuditResultsLegalExport = $AuditResultsLegalExport + $AuditTotals
			
			# Begin the process to get the extracted text file size of all the text files in the exportset
			$QueryText = "SELECT exportDirectory FROM ext.reviewExport WHERE pkReviewExportID = $ExportID"
			$ExportPath = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).exportDirectory
			
			Write-Verbose -Message "Getting the extracted text size for all text files in the Legal Export..."
			$ExtractedTextData = Get-ExtractedTextSize -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -ExportPath $ExportPath -PsScriptPath $PsScriptPath -ExportSet $ExportID -Verbose
			
			$ExtractedTextStatus = $ExtractedTextData[0].ToString()
			If ($ExtractedTextStatus -eq "Failed")
			{
				$GetExtractedTextResults = $GetExtractedTextResults + $ExtractedTextData
				
				Continue
			}
			
			# Check for long file extensions
			$QueryText = @"
SELECT ISNULL(COUNT(nuixMain.pkNuixId), 0) AS TotalLongFileExtension
FROM EXT.nuixFiles nuixFiles with (nolock)
INNER JOIN STAGE.nuixMain nuixMain with (nolock)
ON nuixFiles.topLevelGuid = nuixMain.topLevelGuid
AND nuixMain.nuixFileIndicator = 1 -- Restrict to "Files" that will get a DocID by nuix
WHERE LEN(nuixMain.fExtCorrected) >= 25 -- Restrict to length of 25 or greater; includes any files with an already truncated value
AND nuixMain.fkMediaId = $ExportID
"@

			$LongFileExtCheck = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			# If there is a return value of not equal to 0 then the process will check the export directory for all files with extensions longer 25 characters and truncate
			If ($LongFileExtCheck -ne "0")
			{
				Write-Verbose -Message "Searching NATIVES for file extensions longer than 25 characters and truncating..."
				$LongFileExtData = RunTruncateLongFileExtension -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -exportPath $ExportPath -extLength 25 -PsScriptPath $PsScriptPath -Verbose
				
				$LongFileExtStatus = $LongFileExtData[0].ToString()
				
				If ($LongFileExtStatus -eq "Failed")
				{
					$LongFileExtensionsResults = $LongFileExtensionsResults + $LongFileExtData
					
					# Exit out of loop, do not perform any additional task for this set
					continue
				}			
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data does not exist before creating the case
				$VerifyCaseData = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "CaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose			

				$Verify1 = $VerifyCaseData[0].ToString()
				$Verify2 = $VerifyCaseData[1].ToString()

				# Error Handling for Case Data - Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseData			
				}
				
				# Copy Case files to the Network
				$MoveToNetwork = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToNetwork" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToNetwork[0].ToString()
				$Verify2 = $MoveToNetwork[1].ToString()

				# Error Handling for Case Data - Move To Network
				Write-Host "Move To Network: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToNetwork
				}
			}
			
		} #Foreach

Ok, we have a UI feature, where the user fills out a project code, a mediaID and a review export id, the piece I am reffering to is the the review export, which can have a one to many relationship with media, so the review export id field can be passed comma delimited.

I just want to be able to look ahead, and if the next id is related to the same media id as the last, go ahead and kick of a new export without running the RunCaseManagement function…

the following is the code block, for the ForEach loop, in regards to the review export

Foreach ($ReviewExport in $ExportList)
		{
			$QueryText = @"
SELECT MIN(nf.fkMediaId) AS MediaID, re.pkReviewExportId AS pkReviewExportID
FROM EXT.nuixFiles nf
INNER JOIN EXT.reviewExport re
ON nf.fkReviewExportId = re.pkReviewExportId
WHERE re.reviewExport = '$ReviewExport'
GROUP BY re.pkReviewExportId
"@

			# Retrieve MediaID for current $ReviewExport
			$Result = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText)
			$EvidSet = $Result.MediaID
			$ExportID = $Result.pkReviewExportID			
			$ServerInstance = $ProjConnString.ServerName
			$ProjectDBName = $ProjConnString.DatabaseName
			
			# Check for exportID already existing in EXT.nuixProd, if so stop and send notification
			$QueryText = "select ISNULL(COUNT(*), 0) as value from ext.nuixprod WHERE exportID = $ExportID"
			$ExportExist = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			Write-Verbose -Message "Checking if ExportID, $ExportID, is in EXT.nuixProd..."
			If ($ExportExist -ne "0")
			{
				$ExportExistResults = $ExportExistResults + "ExportID, $ExportID, Already exists in EXT.nuixProd."
				
				continue
			}
			Else
			{
				Write-Verbose -Message "ExportID, $ExportID, does not exist in EXT.nuixProd...Continuing with Legal Export"
			}

			Try
			{
				# Get search available license servers available in the pool, then assign first available if license tests as OK
				$QueryResult = Get-QueryOutput -ServerInstance $CentralServerInstace -DatabaseName $CentralDatabase -QueryText "SELECT ServerNames FROM   [ADMIN].[GetNuixLicenseServerList]($DomainCampusID, '$HostNuixVersion');"
				$NewResults = ($QueryResult.ServerNames).ToLower()
				
				# Need the LicenseServernames variable to be an ArrayList
				#   to leverage the Remove() and Insert() functions
				[System.Collections.ArrayList]$LicenseServernames = $NewResults -split ","

				# Move local machine name to the front of the ArrayList of machines to check
				# IF it is present in the list of LicenseServernames
				If ($LicenseServernames -contains ([System.Environment]::MachineName).ToLower())
				{
					$LicenseServernames.Remove([System.Environment]::MachineName.ToLower())
					$LicenseServernames.Insert(0, [System.Environment]::MachineName.ToLower())
				}
				$NUIXLicenseServername = Get-NuixLicenseStatus -ServerList $LicenseServernames -HostNuixVersion $HostNuixVersion -NuixConsoleApp $NuixConsoleApp -SmtpServer $SmtpServer -Verbose
				$LicenseInfo1 = $NUIXLicenseServername[0].ToString()
				If ($LicenseInfo1 -eq "Failed")
				{
					$LicenseResults = $LicenseResults + $NUIXLicenseServername[1].ToString()
					continue
				}
			}
			Catch
			{
				$LicenseResults = $LicenseResults + "THERE ARE NO NUIX LICENSE SERVERS AVAILABLE! Please contact Client Applications to get the Nuix License updated and made available in the pool...`n"
				continue
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data for legal exports does exist before creating the case
				$VerifyCaseDataLegalExports = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "LegalExportCaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose
				
				$Verify1 = $VerifyCaseDataLegalExports[0].ToString()
				$Verify2 = $VerifyCaseDataLegalExports[1].ToString()
				
				# Error Handling for Case Data - Legal Export Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseDataLegalExports
			
					# stopping operation on this specid and going to the next iteration
					continue				
				}				
				
				# Copy Case files to the local Case directory
				$MoveToLocal = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToLocal" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToLocal[0].ToString()
				$Verify2 = $MoveToLocal[1].ToString()

				# Error Handling for Case Data - Move To Local
				Write-Host "Move To Local: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToLocal				

					# stopping operation on this specid and going to the next iteration
					continue				
				}
			}

			# Run OCR IF enabled for project and supported by nuix version; Must happen before Legal Export
			if (($NuixVersion.propertyValue.ToLower() -ge "v5.2") -and ($OCRSetting.propertyValue.ToLower() -eq "nuix"))
			{	

				$ParamList = @{
				'PsScriptPath' = "$PsScriptPath\Functions";
				'ServerInstance' = ($ProjConnString.ServerName);
				'DatabaseName' = ($ProjConnString.DatabaseName);
				'EvidenceSet' = $EvidSet;
				'InstalledNuixVersion' = "$HostNuixVersion";
				'NuixApplication' = "$NuixConsoleApp";
				'SessionNum' = $SessionNum;
				'SettingCategory' = "8030B5D7-CEC9-439B-BACC-07298531A7E4";
				'NuixOperation' = 'OCR';
				'ExportSet' = $ReviewExport;
				'LogsDirectory' = $LogsDirectory; 
				'TempDirectory' = $TempDirectory;
				'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

				$AuditTotals = RunNuixOperation @ParamList -Verbose
				
				$AuditResultsOCR = $AuditResultsOCR + $AuditTotals				
				
			} #IF OCR Enabled	
			
			# Run Legal Export
			$ParamList = @{
			'PsScriptPath' = "$PsScriptPath\Functions";
			'ServerInstance' = ($ProjConnString.ServerName);
			'DatabaseName' = ($ProjConnString.DatabaseName);
			'EvidenceSet' = $EvidSet;
			'InstalledNuixVersion' = "$HostNuixVersion";
			'NuixApplication' = "$NuixConsoleApp";
			'SessionNum' = $SessionNum;
			'SettingCategory' = "BEC8DE37-EB11-4FDC-B6A6-057D53CCB45F";
			'NuixOperation' = 'LegalExport';
			'ExportSet' = $ReviewExport;
			'LogsDirectory' = $LogsDirectory; 
			'TempDirectory' = $TempDirectory;
			'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

			$AuditTotals = RunNuixOperation @ParamList -Verbose

			$AuditResultsLegalExport = $AuditResultsLegalExport + $AuditTotals
			
			# Begin the process to get the extracted text file size of all the text files in the exportset
			$QueryText = "SELECT exportDirectory FROM ext.reviewExport WHERE pkReviewExportID = $ExportID"
			$ExportPath = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).exportDirectory
			
			Write-Verbose -Message "Getting the extracted text size for all text files in the Legal Export..."
			$ExtractedTextData = Get-ExtractedTextSize -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -ExportPath $ExportPath -PsScriptPath $PsScriptPath -ExportSet $ExportID -Verbose
			
			$ExtractedTextStatus = $ExtractedTextData[0].ToString()
			If ($ExtractedTextStatus -eq "Failed")
			{
				$GetExtractedTextResults = $GetExtractedTextResults + $ExtractedTextData
				
				Continue
			}
			
			# Check for long file extensions
			$QueryText = @"
SELECT ISNULL(COUNT(nuixMain.pkNuixId), 0) AS TotalLongFileExtension
FROM EXT.nuixFiles nuixFiles with (nolock)
INNER JOIN STAGE.nuixMain nuixMain with (nolock)
ON nuixFiles.topLevelGuid = nuixMain.topLevelGuid
AND nuixMain.nuixFileIndicator = 1 -- Restrict to "Files" that will get a DocID by nuix
WHERE LEN(nuixMain.fExtCorrected) >= 25 -- Restrict to length of 25 or greater; includes any files with an already truncated value
AND nuixMain.fkMediaId = $ExportID
"@

			$LongFileExtCheck = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			# If there is a return value of not equal to 0 then the process will check the export directory for all files with extensions longer 25 characters and truncate
			If ($LongFileExtCheck -ne "0")
			{
				Write-Verbose -Message "Searching NATIVES for file extensions longer than 25 characters and truncating..."
				$LongFileExtData = RunTruncateLongFileExtension -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -exportPath $ExportPath -extLength 25 -PsScriptPath $PsScriptPath -Verbose
				
				$LongFileExtStatus = $LongFileExtData[0].ToString()
				
				If ($LongFileExtStatus -eq "Failed")
				{
					$LongFileExtensionsResults = $LongFileExtensionsResults + $LongFileExtData
					
					# Exit out of loop, do not perform any additional task for this set
					continue
				}			
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data does not exist before creating the case
				$VerifyCaseData = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "CaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose			

				$Verify1 = $VerifyCaseData[0].ToString()
				$Verify2 = $VerifyCaseData[1].ToString()

				# Error Handling for Case Data - Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseData			
				}
				
				# Copy Case files to the Network
				$MoveToNetwork = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToNetwork" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToNetwork[0].ToString()
				$Verify2 = $MoveToNetwork[1].ToString()

				# Error Handling for Case Data - Move To Network
				Write-Host "Move To Network: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToNetwork
				}
			}
			
		} #Foreach

I’ve tried submitting twice, for some reason it’s not showing up…

Ok, we have a UI feature, where the user fills out a project code, a mediaID and a review export id, the piece I am refering to is the the review export, which can have a one to many relationship with media, so the review export id field can be passed comma delimited.

I just want to be able to look ahead, and if the next id is related to the same media id as the last, go ahead and kick of a new export without running the RunCaseManagement function…

the following is the code block, for the ForEach loop, in regards to the review export

Foreach ($ReviewExport in $ExportList)
		{
			$QueryText = @"
SELECT MIN(nf.fkMediaId) AS MediaID, re.pkReviewExportId AS pkReviewExportID
FROM EXT.nuixFiles nf
INNER JOIN EXT.reviewExport re
ON nf.fkReviewExportId = re.pkReviewExportId
WHERE re.reviewExport = '$ReviewExport'
GROUP BY re.pkReviewExportId
"@

			# Retrieve MediaID for current $ReviewExport
			$Result = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText)
			$EvidSet = $Result.MediaID
			$ExportID = $Result.pkReviewExportID			
			$ServerInstance = $ProjConnString.ServerName
			$ProjectDBName = $ProjConnString.DatabaseName
			
			# Check for exportID already existing in EXT.nuixProd, if so stop and send notification
			$QueryText = "select ISNULL(COUNT(*), 0) as value from ext.nuixprod WHERE exportID = $ExportID"
			$ExportExist = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			Write-Verbose -Message "Checking if ExportID, $ExportID, is in EXT.nuixProd..."
			If ($ExportExist -ne "0")
			{
				$ExportExistResults = $ExportExistResults + "ExportID, $ExportID, Already exists in EXT.nuixProd."
				
				continue
			}
			Else
			{
				Write-Verbose -Message "ExportID, $ExportID, does not exist in EXT.nuixProd...Continuing with Legal Export"
			}

			Try
			{
				# Get search available license servers available in the pool, then assign first available if license tests as OK
				$QueryResult = Get-QueryOutput -ServerInstance $CentralServerInstace -DatabaseName $CentralDatabase -QueryText "SELECT ServerNames FROM   [ADMIN].[GetNuixLicenseServerList]($DomainCampusID, '$HostNuixVersion');"
				$NewResults = ($QueryResult.ServerNames).ToLower()
				
				# Need the LicenseServernames variable to be an ArrayList
				#   to leverage the Remove() and Insert() functions
				[System.Collections.ArrayList]$LicenseServernames = $NewResults -split ","

				# Move local machine name to the front of the ArrayList of machines to check
				# IF it is present in the list of LicenseServernames
				If ($LicenseServernames -contains ([System.Environment]::MachineName).ToLower())
				{
					$LicenseServernames.Remove([System.Environment]::MachineName.ToLower())
					$LicenseServernames.Insert(0, [System.Environment]::MachineName.ToLower())
				}
				$NUIXLicenseServername = Get-NuixLicenseStatus -ServerList $LicenseServernames -HostNuixVersion $HostNuixVersion -NuixConsoleApp $NuixConsoleApp -SmtpServer $SmtpServer -Verbose
				$LicenseInfo1 = $NUIXLicenseServername[0].ToString()
				If ($LicenseInfo1 -eq "Failed")
				{
					$LicenseResults = $LicenseResults + $NUIXLicenseServername[1].ToString()
					continue
				}
			}
			Catch
			{
				$LicenseResults = $LicenseResults + "THERE ARE NO NUIX LICENSE SERVERS AVAILABLE! Please contact Client Applications to get the Nuix License updated and made available in the pool...`n"
				continue
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data for legal exports does exist before creating the case
				$VerifyCaseDataLegalExports = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "LegalExportCaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose
				
				$Verify1 = $VerifyCaseDataLegalExports[0].ToString()
				$Verify2 = $VerifyCaseDataLegalExports[1].ToString()
				
				# Error Handling for Case Data - Legal Export Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseDataLegalExports
			
					# stopping operation on this specid and going to the next iteration
					continue				
				}				
				
				# Copy Case files to the local Case directory
				$MoveToLocal = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToLocal" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToLocal[0].ToString()
				$Verify2 = $MoveToLocal[1].ToString()

				# Error Handling for Case Data - Move To Local
				Write-Host "Move To Local: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToLocal				

					# stopping operation on this specid and going to the next iteration
					continue				
				}
			}

			# Run OCR IF enabled for project and supported by nuix version; Must happen before Legal Export
			if (($NuixVersion.propertyValue.ToLower() -ge "v5.2") -and ($OCRSetting.propertyValue.ToLower() -eq "nuix"))
			{	

				$ParamList = @{
				'PsScriptPath' = "$PsScriptPath\Functions";
				'ServerInstance' = ($ProjConnString.ServerName);
				'DatabaseName' = ($ProjConnString.DatabaseName);
				'EvidenceSet' = $EvidSet;
				'InstalledNuixVersion' = "$HostNuixVersion";
				'NuixApplication' = "$NuixConsoleApp";
				'SessionNum' = $SessionNum;
				'SettingCategory' = "8030B5D7-CEC9-439B-BACC-07298531A7E4";
				'NuixOperation' = 'OCR';
				'ExportSet' = $ReviewExport;
				'LogsDirectory' = $LogsDirectory; 
				'TempDirectory' = $TempDirectory;
				'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

				$AuditTotals = RunNuixOperation @ParamList -Verbose
				
				$AuditResultsOCR = $AuditResultsOCR + $AuditTotals				
				
			} #IF OCR Enabled	
			
			# Run Legal Export
			$ParamList = @{
			'PsScriptPath' = "$PsScriptPath\Functions";
			'ServerInstance' = ($ProjConnString.ServerName);
			'DatabaseName' = ($ProjConnString.DatabaseName);
			'EvidenceSet' = $EvidSet;
			'InstalledNuixVersion' = "$HostNuixVersion";
			'NuixApplication' = "$NuixConsoleApp";
			'SessionNum' = $SessionNum;
			'SettingCategory' = "BEC8DE37-EB11-4FDC-B6A6-057D53CCB45F";
			'NuixOperation' = 'LegalExport';
			'ExportSet' = $ReviewExport;
			'LogsDirectory' = $LogsDirectory; 
			'TempDirectory' = $TempDirectory;
			'NUIXLicenseServerName' = $NUIXLicenseServerName 
			}

			$AuditTotals = RunNuixOperation @ParamList -Verbose

			$AuditResultsLegalExport = $AuditResultsLegalExport + $AuditTotals
			
			# Begin the process to get the extracted text file size of all the text files in the exportset
			$QueryText = "SELECT exportDirectory FROM ext.reviewExport WHERE pkReviewExportID = $ExportID"
			$ExportPath = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).exportDirectory
			
			Write-Verbose -Message "Getting the extracted text size for all text files in the Legal Export..."
			$ExtractedTextData = Get-ExtractedTextSize -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -ExportPath $ExportPath -PsScriptPath $PsScriptPath -ExportSet $ExportID -Verbose
			
			$ExtractedTextStatus = $ExtractedTextData[0].ToString()
			If ($ExtractedTextStatus -eq "Failed")
			{
				$GetExtractedTextResults = $GetExtractedTextResults + $ExtractedTextData
				
				Continue
			}
			
			# Check for long file extensions
			$QueryText = @"
SELECT ISNULL(COUNT(nuixMain.pkNuixId), 0) AS TotalLongFileExtension
FROM EXT.nuixFiles nuixFiles with (nolock)
INNER JOIN STAGE.nuixMain nuixMain with (nolock)
ON nuixFiles.topLevelGuid = nuixMain.topLevelGuid
AND nuixMain.nuixFileIndicator = 1 -- Restrict to "Files" that will get a DocID by nuix
WHERE LEN(nuixMain.fExtCorrected) >= 25 -- Restrict to length of 25 or greater; includes any files with an already truncated value
AND nuixMain.fkMediaId = $ExportID
"@

			$LongFileExtCheck = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
			
			# If there is a return value of not equal to 0 then the process will check the export directory for all files with extensions longer 25 characters and truncate
			If ($LongFileExtCheck -ne "0")
			{
				Write-Verbose -Message "Searching NATIVES for file extensions longer than 25 characters and truncating..."
				$LongFileExtData = RunTruncateLongFileExtension -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -exportPath $ExportPath -extLength 25 -PsScriptPath $PsScriptPath -Verbose
				
				$LongFileExtStatus = $LongFileExtData[0].ToString()
				
				If ($LongFileExtStatus -eq "Failed")
				{
					$LongFileExtensionsResults = $LongFileExtensionsResults + $LongFileExtData
					
					# Exit out of loop, do not perform any additional task for this set
					continue
				}			
			}

			# If Nuix Version is 4.2 or higher then case management will be initiated
			If ($HostNuixVersion.ToLower() -ge 'v4.2') 
			{			
				# Verify case data does not exist before creating the case
				$VerifyCaseData = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "CaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose			

				$Verify1 = $VerifyCaseData[0].ToString()
				$Verify2 = $VerifyCaseData[1].ToString()

				# Error Handling for Case Data - Case Data Exists
				Write-Host "Verification of Case Data: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $VerifyCaseData			
				}
				
				# Copy Case files to the Network
				$MoveToNetwork = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToNetwork" "$PsScriptPath" "$FastCopyAppPath" -Verbose

				$Verify1 = $MoveToNetwork[0].ToString()
				$Verify2 = $MoveToNetwork[1].ToString()

				# Error Handling for Case Data - Move To Network
				Write-Host "Move To Network: $Verify1, $Verify2"
				
				If ($Verify1 -eq "Failed")
				{
					$VerifyResults = $VerifyResults + $MoveToNetwork
				}
			}
			
		} #Foreach

Ok, we have a UI feature, where the user fills out a project code, a mediaID and a review export id, the piece I am refering to is the the review export, which can have a one to many relationship with media, so the review export id field can be passed comma delimited.

I just want to be able to look ahead, and if the next id is related to the same media id as the last, go ahead and kick of a new export without running the RunCaseManagement function…

the following is the code block, for the ForEach loop, in regards to the review export

Foreach ($ReviewExport in $ExportList) { $QueryText = @" SELECT MIN(nf.fkMediaId) AS MediaID, re.pkReviewExportId AS pkReviewExportID FROM EXT.nuixFiles nf INNER JOIN EXT.reviewExport re ON nf.fkReviewExportId = re.pkReviewExportId WHERE re.reviewExport = '$ReviewExport' GROUP BY re.pkReviewExportId "@
		# Retrieve MediaID for current $ReviewExport
		$Result = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText)
		$EvidSet = $Result.MediaID
		$ExportID = $Result.pkReviewExportID			
		$ServerInstance = $ProjConnString.ServerName
		$ProjectDBName = $ProjConnString.DatabaseName
		
		# Check for exportID already existing in EXT.nuixProd, if so stop and send notification
		$QueryText = "select ISNULL(COUNT(*), 0) as value from ext.nuixprod WHERE exportID = $ExportID"
		$ExportExist = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
		
		Write-Verbose -Message "Checking if ExportID, $ExportID, is in EXT.nuixProd..."
		If ($ExportExist -ne "0")
		{
			$ExportExistResults = $ExportExistResults + "ExportID, $ExportID, Already exists in EXT.nuixProd."
			
			continue
		}
		Else
		{
			Write-Verbose -Message "ExportID, $ExportID, does not exist in EXT.nuixProd...Continuing with Legal Export"
		}

		Try
		{
			# Get search available license servers available in the pool, then assign first available if license tests as OK
			$QueryResult = Get-QueryOutput -ServerInstance $CentralServerInstace -DatabaseName $CentralDatabase -QueryText "SELECT ServerNames FROM   [ADMIN].[GetNuixLicenseServerList]($DomainCampusID, '$HostNuixVersion');"
			$NewResults = ($QueryResult.ServerNames).ToLower()
			
			# Need the LicenseServernames variable to be an ArrayList
			#   to leverage the Remove() and Insert() functions
			[System.Collections.ArrayList]$LicenseServernames = $NewResults -split ","

			# Move local machine name to the front of the ArrayList of machines to check
			# IF it is present in the list of LicenseServernames
			If ($LicenseServernames -contains ([System.Environment]::MachineName).ToLower())
			{
				$LicenseServernames.Remove([System.Environment]::MachineName.ToLower())
				$LicenseServernames.Insert(0, [System.Environment]::MachineName.ToLower())
			}
			$NUIXLicenseServername = Get-NuixLicenseStatus -ServerList $LicenseServernames -HostNuixVersion $HostNuixVersion -NuixConsoleApp $NuixConsoleApp -SmtpServer $SmtpServer -Verbose
			$LicenseInfo1 = $NUIXLicenseServername[0].ToString()
			If ($LicenseInfo1 -eq "Failed")
			{
				$LicenseResults = $LicenseResults + $NUIXLicenseServername[1].ToString()
				continue
			}
		}
		Catch
		{
			$LicenseResults = $LicenseResults + "THERE ARE NO NUIX LICENSE SERVERS AVAILABLE! Please contact Client Applications to get the Nuix License updated and made available in the pool...`n"
			continue
		}

		# If Nuix Version is 4.2 or higher then case management will be initiated
		If ($HostNuixVersion.ToLower() -ge 'v4.2') 
		{			
			# Verify case data for legal exports does exist before creating the case
			$VerifyCaseDataLegalExports = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "LegalExportCaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose
			
			$Verify1 = $VerifyCaseDataLegalExports[0].ToString()
			$Verify2 = $VerifyCaseDataLegalExports[1].ToString()
			
			# Error Handling for Case Data - Legal Export Case Data Exists
			Write-Host "Verification of Case Data: $Verify1, $Verify2"
			
			If ($Verify1 -eq "Failed")
			{
				$VerifyResults = $VerifyResults + $VerifyCaseDataLegalExports
		
				# stopping operation on this specid and going to the next iteration
				continue				
			}				
			
			# Copy Case files to the local Case directory
			$MoveToLocal = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToLocal" "$PsScriptPath" "$FastCopyAppPath" -Verbose

			$Verify1 = $MoveToLocal[0].ToString()
			$Verify2 = $MoveToLocal[1].ToString()

			# Error Handling for Case Data - Move To Local
			Write-Host "Move To Local: $Verify1, $Verify2"
			
			If ($Verify1 -eq "Failed")
			{
				$VerifyResults = $VerifyResults + $MoveToLocal				

				# stopping operation on this specid and going to the next iteration
				continue				
			}
		}

		# Run OCR IF enabled for project and supported by nuix version; Must happen before Legal Export
		if (($NuixVersion.propertyValue.ToLower() -ge "v5.2") -and ($OCRSetting.propertyValue.ToLower() -eq "nuix"))
		{	

			$ParamList = @{
			'PsScriptPath' = "$PsScriptPath\Functions";
			'ServerInstance' = ($ProjConnString.ServerName);
			'DatabaseName' = ($ProjConnString.DatabaseName);
			'EvidenceSet' = $EvidSet;
			'InstalledNuixVersion' = "$HostNuixVersion";
			'NuixApplication' = "$NuixConsoleApp";
			'SessionNum' = $SessionNum;
			'SettingCategory' = "8030B5D7-CEC9-439B-BACC-07298531A7E4";
			'NuixOperation' = 'OCR';
			'ExportSet' = $ReviewExport;
			'LogsDirectory' = $LogsDirectory; 
			'TempDirectory' = $TempDirectory;
			'NUIXLicenseServerName' = $NUIXLicenseServerName 
		}

			$AuditTotals = RunNuixOperation @ParamList -Verbose
			
			$AuditResultsOCR = $AuditResultsOCR + $AuditTotals				
			
		} #IF OCR Enabled	
		
		# Run Legal Export
		$ParamList = @{
		'PsScriptPath' = "$PsScriptPath\Functions";
		'ServerInstance' = ($ProjConnString.ServerName);
		'DatabaseName' = ($ProjConnString.DatabaseName);
		'EvidenceSet' = $EvidSet;
		'InstalledNuixVersion' = "$HostNuixVersion";
		'NuixApplication' = "$NuixConsoleApp";
		'SessionNum' = $SessionNum;
		'SettingCategory' = "BEC8DE37-EB11-4FDC-B6A6-057D53CCB45F";
		'NuixOperation' = 'LegalExport';
		'ExportSet' = $ReviewExport;
		'LogsDirectory' = $LogsDirectory; 
		'TempDirectory' = $TempDirectory;
		'NUIXLicenseServerName' = $NUIXLicenseServerName 
		}

		$AuditTotals = RunNuixOperation @ParamList -Verbose

		$AuditResultsLegalExport = $AuditResultsLegalExport + $AuditTotals
		
		# Begin the process to get the extracted text file size of all the text files in the exportset
		$QueryText = "SELECT exportDirectory FROM ext.reviewExport WHERE pkReviewExportID = $ExportID"
		$ExportPath = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).exportDirectory
		
		Write-Verbose -Message "Getting the extracted text size for all text files in the Legal Export..."
		$ExtractedTextData = Get-ExtractedTextSize -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -ExportPath $ExportPath -PsScriptPath $PsScriptPath -ExportSet $ExportID -Verbose
		
		$ExtractedTextStatus = $ExtractedTextData[0].ToString()
		If ($ExtractedTextStatus -eq "Failed")
		{
			$GetExtractedTextResults = $GetExtractedTextResults + $ExtractedTextData
			
			Continue
		}
		
		# Check for long file extensions
		$QueryText = @"

SELECT ISNULL(COUNT(nuixMain.pkNuixId), 0) AS TotalLongFileExtension
FROM EXT.nuixFiles nuixFiles with (nolock)
INNER JOIN STAGE.nuixMain nuixMain with (nolock)
ON nuixFiles.topLevelGuid = nuixMain.topLevelGuid
AND nuixMain.nuixFileIndicator = 1 – Restrict to “Files” that will get a DocID by nuix
WHERE LEN(nuixMain.fExtCorrected) >= 25 – Restrict to length of 25 or greater; includes any files with an already truncated value
AND nuixMain.fkMediaId = $ExportID
"@

		$LongFileExtCheck = (Get-QueryOutput ($ProjConnString.ServerName) ($ProjConnString.DatabaseName) $QueryText).value
		
		# If there is a return value of not equal to 0 then the process will check the export directory for all files with extensions longer 25 characters and truncate
		If ($LongFileExtCheck -ne "0")
		{
			Write-Verbose -Message "Searching NATIVES for file extensions longer than 25 characters and truncating..."
			$LongFileExtData = RunTruncateLongFileExtension -ServerInstance ($ProjConnString.ServerName) -ProjDatabase ($ProjConnString.DatabaseName) -exportPath $ExportPath -extLength 25 -PsScriptPath $PsScriptPath -Verbose
			
			$LongFileExtStatus = $LongFileExtData[0].ToString()
			
			If ($LongFileExtStatus -eq "Failed")
			{
				$LongFileExtensionsResults = $LongFileExtensionsResults + $LongFileExtData
				
				# Exit out of loop, do not perform any additional task for this set
				continue
			}			
		}

		# If Nuix Version is 4.2 or higher then case management will be initiated
		If ($HostNuixVersion.ToLower() -ge 'v4.2') 
		{			
			# Verify case data does not exist before creating the case
			$VerifyCaseData = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "VerifyCaseData" "CaseData-Exist" "$PsScriptPath" "$FastCopyAppPath" -Verbose			

			$Verify1 = $VerifyCaseData[0].ToString()
			$Verify2 = $VerifyCaseData[1].ToString()

			# Error Handling for Case Data - Case Data Exists
			Write-Host "Verification of Case Data: $Verify1, $Verify2"
			
			If ($Verify1 -eq "Failed")
			{
				$VerifyResults = $VerifyResults + $VerifyCaseData			
			}
			
			# Copy Case files to the Network
			$MoveToNetwork = RunCaseManagmentTool $ServerInstance $ProjectDBName $EvidSet $LogsDirectory "CaseCreate" "ToNetwork" "$PsScriptPath" "$FastCopyAppPath" -Verbose

			$Verify1 = $MoveToNetwork[0].ToString()
			$Verify2 = $MoveToNetwork[1].ToString()

			# Error Handling for Case Data - Move To Network
			Write-Host "Move To Network: $Verify1, $Verify2"
			
			If ($Verify1 -eq "Failed")
			{
				$VerifyResults = $VerifyResults + $MoveToNetwork
			}
		}
		
	} #Foreach

Ok, we have a UI feature, where the user fills out a project code, a mediaID and a review export id, the piece I am refering to is the the review export, which can have a one to many relationship with media, so the review export id field can be passed comma delimited.

I just want to be able to look ahead, and if the next id is related to the same media id as the last, go ahead and kick of a new export without running the RunCaseManagement function…

the following is the code block, for the ForEach loop, in regards to the review export

Foreach ($ReviewExport in $ExportList)
		{
		# A lot happens in here, but it is all based on the values in the $ExportList
			
		} #Foreach

I was not able to paste the whole script, I guess there is a limitation…

You will probably need to use “for” with a counter variable like $i instead of “foreach”:

for ($i = 0; $i -lt $ExportList.Count; $i++)
{
    # A lot happens in here, but it is all based on the values in the $ExportList
    $ReviewExport = $ExportList[$i]
    "Current: $ReviewExport"

    # Look ahead only if not the last item
    if ($i -lt ($ExportList.Count-1))
    {
        $ReviewExportNext = $ExportList[$i+1]
        "Next: $ReviewExportNext"
    }
} #Foreach

Rob asked for a sample of the data. That’s entirely appropriate. Because at this this point we can give you sort of broad and gnerall guidelines, but the likelihood of nailing a solution based on what you’ve told us is minimal. This happens so often, I have a generic request.

In the future, please post several lines of “real” data,
sanitized as necessary. It’s been our overwhelming
experience that made up sample data (though it makes
perfect sense in the mind of the original poster) is
rarely ever accurate. This results in multiple iterations
trying to get to something that works with the real data.
This just wastes time and frustrates everyone. Your
cooperation is greatly appreciated.