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