I am migrating the script below to a 2k12r2 server. I noticed after I moved it that it's consuming ALOT of memory. I have started reading about stringbuilder and how it can streamline scripts with alot of strings. Can someone give me an example of how I can incorporate stringbuilder to streamline the script below? I just need to get pointed in the right direction.
start-transcript -path ./BBBBXfer.log -append
$DN = "LDAP://ldapserver/dc=bbbb,dc=pvt"
$username = "uid=ldapuser,cn=service accounts,cn=config"
$password = "ldappassword"
$auth = [System.DirectoryServices.AuthenticationTypes]::None
$de = New-Object System.DirectoryServices.DirectoryEntry($dn,$username,$password,$auth)
$array = @()
$Header = 'bbbbcn' + "`t"
$Header += 'bbbbCompanyID' + "`t"
$Header += 'bbbbCompanyName' + "`t"
$Header += 'bbbbEffectiveStartDate' + "`t"
$Header += 'bbbbEmpExemptStatus' + "`t"
$Header += 'bbbbEmpRehireDate' + "`t"
$Header += 'bbbbGivenName' + "`t"
$Header += 'bbbbInitials' + "`t"
$Header += 'bbbbLegUserID' + "`t"
$Header += 'bbbbMail' + "`t"
$Header += 'bbbbMgrID' + "`t"
$Header += 'bbbbManagerName' + "`t"
$Header += 'bbbbPosAccountingUnit' + "`t"
$Header += 'bbbbPosDAUDescription' + "`t"
$Header += 'bbbbPosDepartmentFunctionName' + "`t"
$Header += 'bbbbPosDepartmentID' + "`t"
$Header += 'bbbbPosDepartmentName' + "`t"
$Header += 'bbbbPosEmployeeID' + "`t"
$Header += 'bbbbPosJobCode' + "`t"
$Header += 'bbbbPosJobFamilyDescriptor' + "`t"
$Header += 'bbbbPosLocationDescription' + "`t"
$Header += 'bbbbPosOneOverManagerID' + "`t"
$Header += 'bbbbPosOneOverManagerName' + "`t"
$Header += 'bbbbPosPositionTitle' + "`t"
$Header += 'bbbbPosTwoOverManagerID' + "`t"
$Header += 'bbbbPosTwoOverManagerName' + "`t"
$Header += 'bbbbProcessLevel' + "`t"
$Header += 'bbbbSn' + "`t"
$Header += 'bbbbStatus' + "`t"
$Header += 'bbbbEffectiveEndDate' + "`t"
$Header += 'bbbbEmpEmployeeID' + "`t"
Add-Content bbbbDailyExport.txt $Header
$query = "(|(bbbbenterpriseID=e*)(bbbbenterpriseID=p*)(bbbbenterpriseID=c*)(bbbbenterpriseID=a*))"
$ds = New-Object system.DirectoryServices.DirectorySearcher($de,$query)
$ds.PropertiesToLoad.Add("bbbbcn") | Out-Null
$ds.PropertiesToLoad.Add("bbbbCompanyID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbCompanyName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEffectiveStartDate") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEmpExemptStatus") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEmpRehireDate") | Out-Null
$ds.PropertiesToLoad.Add("bbbbGivenName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbInitials") | Out-Null
$ds.PropertiesToLoad.Add("bbbbLegUserID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbMail") | Out-Null
$ds.PropertiesToLoad.Add("bbbbMgrID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbManagerName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosAccountingUnit") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosDAUDescription") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosDepartmentFunctionName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosDepartmentID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosDepartmentName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEnterpriseID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosJobCode") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosJobFamilyDescriptor") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosLocationDescription") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosOneOverManagerID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosOneOverManagerName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosPositionTitle") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosTwoOverManagerID") | Out-Null
$ds.PropertiesToLoad.Add("bbbbPosTwoOverManagerName") | Out-Null
$ds.PropertiesToLoad.Add("bbbbProcessLevel") | Out-Null
$ds.PropertiesToLoad.Add("bbbbSn") | Out-Null
$ds.PropertiesToLoad.Add("bbbbStatus") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEffectiveEndDate") | Out-Null
$ds.PropertiesToLoad.Add("bbbbEmpEmployeeID") | Out-Null foreach($result in $ds.Findall()){
$samAccountName = $result.GetDirectoryEntry().bbbbLegUserID.ToString()
$bbbbStatus = $result.GetDirectoryEntry().bbbbstatus.ToString()
$Name = $user.Name
$export = new-object psobject
$Line = ""
$Line = $result.GetDirectoryEntry().bbbbcn.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbCompanyID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbCompanyName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEffectiveStartDate.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEmpExemptStatus.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEmpRehireDate.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbGivenName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbInitials.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbLegUserID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbMail.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbMgrID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbManagerName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosAccountingUnit.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosDAUDescription.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosDepartmentFunctionName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosDepartmentID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosDepartmentName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEnterpriseID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosJobCode.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosJobFamilyDescriptor.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosLocationDescription.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosOneOverManagerID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosOneOverManagerName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosPositionTitle.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosTwoOverManagerID.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbPosTwoOverManagerName.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbProcessLevel.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbSn.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbStatus.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEffectiveEndDate.ToString() + "`t"
$Line += $result.GetDirectoryEntry().bbbbEmpEmployeeID.ToString() + "`t"
Add-Content bbbbDailyExport.txt $Line
}
Move-Item bbbbDailyExport.txt "D:\Scripts\bbbbExport\bbbbDailyExport-ArchiveTemp\bbbbDailyExport$(Get-date -f yyyyMMdd).txt"
#Move-Item bbbbDailyExport.txt "\\server\Feed\bbbbDailyExport$(Get-date -f yyyyMMdd).txt"
stop-transcript