Advertisement
1RedOne

v2.0 mass string replace

Oct 7th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##V2 is all about using the string builder method instead
  2. <#
  3. 2. Load the initial string into a System.Text.StringBuilder and manipulate the string inside it. StringBuilders are mutable; so, this should be faster. (e.g.
  4. $sb = New-Object System.Text.StringBuilder((C:\Path\MyFile.xml | Out-String))
  5. And then manipulate that with it's .Replace() method.
  6. #>
  7.  
  8. Function Update-OCSXMLv2 {
  9. param(
  10.     $BuddyListFile = "A:\AsiaContactsExport.xml",
  11.     $NameMatchingFile= "C:\Users\sowen\Dropbox\Docs\Foxdeploy\contoso\contoso - Source Disabled Users with OCS Accounts - Disable OCS - 20141002.csv",
  12.     $OutFile = ("A:\temp\" + (get-date -UFormat %Y%m%d_%H%M) + "_UpdatedOSCSXML.xml")
  13. )
  14.  
  15.     begin{
  16.         $i=0
  17.         Write-host "Importing contents of `$BuddyListFile......" -NoNewline
  18.         #original $FileInMemory = Get-Content $BuddyListFile
  19.  
  20.         $FileInMemory = New-Object System.Text.StringBuilder((Get-Content $BuddyListFile | out-string))
  21.  
  22.         Write-host "[OK]" -ForegroundColor Green
  23.         $starttime = get-date
  24.         if ($NameMatchingFile){
  25.             Write-host "Importing contents of `$NameMatchingFile..." -NoNewline #I'm doing two Wheres instead of one, as I think this is faster
  26.             $NameMatchingFile = import-csv $NameMatchingFile | ? {$_."msRTCSIP-PrimaryUserAddress"}| ? {$_.mail} |select @{Name=‘Source‘;Expression={$_.'msRTCSIP-PrimaryUserAddress'}},@{Name=‘Target‘;Expression={$_.mail}}
  27.             Write-host "[OK]" -ForegroundColor Green
  28.         }
  29.     #EndOfBeginning
  30.     }
  31.   process{
  32.        
  33.         if ($NameMatchingFile){
  34.             ForEach ($name in $NameMatchingFile){
  35.                 $i++
  36.                 $nowTime = get-date
  37.                 $projectedTime = ($nowTime - $startTime).Seconds / $i * ($NameMatchingFile.Count - $i)
  38.                 if ($projectedTime -gt [math]::Abs(60)){$projectedText = ([math]::Round(($projectedTime / 60),2), " minutes" -join " ")}
  39.                     ELSE{$projectedText = (([math]::Round($projectedTime,2))," seconds" -join " ")}
  40.                 #"`$projectedTime $projectedTime"
  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.                 #Add Replace $nameA with $nameB
  45.                 $FileInMemory.Replace($name.Source,$name.Target) | out-null
  46.                 #Write-host "[OK]" -ForegroundColor Green
  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.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-OCSXMLv2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement