“The pause doesn’t work for me”, said one of my team members about the pause function in my Dot Source Reminder code. We took some time to analyze why and found that his shell settings were different from mine. Instead I decided to focus on whether the code executed inside the ISE. Next was to update all the affected scripts. Here is what I did:
#Find $Old = @' if (([Environment]::GetCommandLineArgs()) -match '&'){pause} '@ #Replace $New = @' if ([regex]::IsMatch([Environment]::GetCommandLineArgs(), 'powershell_ise') -eq $false){Pause} '@ $searchPath = 'z:\PowerShell\*.ps1' gci $searchPath -Recurse | foreach { $path = [string]$_.Fullname $fName = [string]$_.Name $txt = Get-Content -path $path if([regex]::IsMatch($txt,[regex]::Escape($old))){ Write-Host "Match found in $fName" -BackgroundColor Green $txt.Replace($old,$New) | Set-Content -Path $path -force } ELSE{ Write-Host "Skipping $fName, no match" -BackgroundColor Yellow -ForegroundColor Black } }
A couple of notes. I used the “here string” option for the search and replace text. Next, I used the RegEx escape method to make sure that the replacement code would be properly interpreted. The code above does a search and replace of all of scripts in z:\PowerShell, replacing the old text with the new.