Advertisement
1RedOne

Large in-memory replace becomes slow

Oct 6th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Idea
  2. #Replace values
  3. #Give a CSV with old value, new value
  4. #Replace a single value
  5. #
  6. #
  7.  
  8.  
  9.  
  10. Function Update-OCSXML {
  11. param(
  12.     $BuddyListFile="A:\Export-Updated.xml",
  13.     $NameMatchingFile="C:\Users\sowen\Dropbox\Docs\foxDeploy.com\Contoso\Contoso - Source Disabled Users with OCS Accounts - Disable OCS - 20141002.csv",
  14.     $OutFile = ("A:\temp\" + (get-date -UFormat %Y%m%d_%H%M) + "_UpdatedOSCSXML.xml")
  15. )
  16.  
  17.     begin{
  18.         $i=0
  19.         Write-host "Importing contents of `$BuddyListFile......" -NoNewline
  20.         $FileInMemory = Get-Content $BuddyListFile
  21.         Write-host "[OK]" -ForegroundColor Green
  22.         $starttime = get-date
  23.         if ($NameMatchingFile){
  24.             Write-host "Importing contents of `$NameMatchingFile..." -NoNewline #I'm doing two Where's instead of one, this makes it faster somehow
  25.             $NameMatchingFile = import-csv $NameMatchingFile | ? {$_."msRTCSIP-PrimaryUserAddress"}| ? {$_.mail} |select @{Name=‘Source‘;Expression={$_.'msRTCSIP-PrimaryUserAddress'}},@{Name=‘Target‘;Expression={$_.mail}}
  26.             Write-host "[OK]" -ForegroundColor Green
  27.         }
  28.     #EndOfBeginning
  29.     }
  30.   process{
  31.        
  32.         if ($NameMatchingFile){
  33.             ForEach ($name in $NameMatchingFile){
  34.                 $i++
  35.                 $nowTime = get-date
  36.                 $projectedTime = ($nowTime - $startTime).Seconds / $i * ($NameMatchingFile.Count - $i)
  37.                 if ($projectedTime -gt [math]::Abs(60)){$projectedText = ([math]::Round(($projectedTime / 60),2), " minutes" -join " ")}
  38.                     ELSE{$projectedText = (([math]::Round($projectedTime,2))," seconds" -join " ")}
  39.                
  40.                
  41.                 $percent = [math]::Round(($i/($NameMatchingFile.count)*100),2)
  42.                 Write-Progress -Activity ("Processing record $i of " + $NameMatchingFile.Count + "| Projected time remaining: $projectedText") -PercentComplete $percent -Status "$percent`% Completed" -CurrentOperation ("Replacing " + $name.Source + " with " +$name.Target +"...")
  43.                
  44.                 #Replace $nameA with $nameB
  45.                 $FileInMemory = $FileInMemory -replace $name.Source,$name.Target
  46.                
  47.                 $lastTime = $nowTime
  48.             }
  49.         }
  50.   #EndOfProcess
  51.     }
  52.  
  53.      end{
  54.  
  55.      #Remove references to ContosoAd
  56.         Write-host ("Replacing Contosoad.Contoso.com with Contoso.com..." ) -NoNewline
  57.         $FileInMemory = $FileInMemory -replace 'Contosoad.Contoso.com','Contoso.com'
  58.         Write-host "[OK]" -ForegroundColor Green
  59.  
  60.     "Outputting file $Outfile"
  61.     $FileInMemory | Out-File $OutFile
  62.     }
  63.    
  64. #EndOfFunction
  65. }
  66.  
  67.  
  68. Update-OCSXML
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement