Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Running MySQL Query getting 'System.OutOfMemoryException'

$
0
0

Hi,

I'm running a MySQL Query to update MS AD and output errors to a txt file. The code works fine but after a few minutes I get the error 'System.OutOfMemoryException'.

Below is a short bit of the code.

Function Addmembers {

	$query = "SELECT * FROM allstudents_groups"

	$dtResult = New-Object "System.Data.DataTable"
	$hashOptions = @{ } 
	$hashOptions["strServer"] = "servername"
	$hashOptions["strDatabase"] = "DB"	
	$hashOptions["strUser"] = "user"
	$hashOptions["strPassword"] = "pass"
	$hashOptions["strQuery"] = $query
	$dtResult = executeMYSQLQuery($hashOptions)	
        $OU = "OU=Class Group,DC=company,DC=internal"


    
    ForEach ($row in $dtResult){
    $result = $row.GroupName
    $resmember = $row.Members
    $out_file = "C:\Scripts\Users.txt"
    $studentOU = "OU=Users,OU=CHS,OU=Est,DC=Company,DC=internal"


    $resmember | %{$_.Split(',')} | %{
    If(Get-QADUser -SearchRoot "$studentOU" -enabled -LdapFilter "(name=$_)")
    {}
    else
    {"$_" | out-file "$out_file" -append}

	}}
	$dtResult = $Null
}

Addmembers

I believe this is due to the code getting all the DB stuff and trying to hold it in memory. I think a ForEach-Object would stop the memory from filling up but that doesn't seem to support logic. I've also tried adding a [System.GC]::Collect() within the IF statement but that does nothing to stop the RAM from creeping up


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>