Vote count: 0
So I've been tasked with creating a script to find and replace strings across our stack for a database change here in the future.
This has me very cautious as how destructive the script could be if it's not run 100% Correctly.
I'm looking for some feedback and code review I guess.
I've already taken some steps to protect the stack (commented out part which replaces strings) but wanted to see what you guys thought.
For Info all Applications are either on D: or E:
$servers = Get-Content "servers.txt"
$filename = "web.config"
$file = "C:\temp\FoundFiles.txt"
$connectionstring1 = "<add key="DBConnection" value="server=QADB1;uid=ID1;pwd=Password;database=Database1"/>"
$connectionstring2 = "<add key="DBConnection" value="server=QADB2;uid=ID1;pwd=Password;database=Database1"/>"
foreach ($computer in $servers) {
$DConfigs += Get-ChildItem -Recurse -Force \\$computer\d$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -eq "$filename") -and ($_.Fullname -notlike "*Recycle.bin*") } | Select-Object fullname | Select-String "connectionstring" | out-file C:\temp\FoundFiles.txt -append
foreach ($DConfig in $DConfigs) {
#(Get-Content $DConfig).replace('connectionstring1', 'connectionstring2') | Out-file 'C:\NewConfigs\' + $EConfig
$ChangedConfig += $DConfig
}
$EConfigs += Get-ChildItem -Recurse -Force \\$computer\e$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -eq "$filename") -and ($_.Fullname -notlike "*Recycle.bin*") } | Select-Object fullname | select-string "connectionstring" | out-file C:\temp\FoundFiles.txt -append
foreach ($EConfig in $EConfigs) {
#(Get-Content $EConfig).replace('connectionstring1', 'connectionstring2') | Out-file 'C:\NewConfigs\' + $EConfig
$ChangedConfig += $EConfig
}
}
$smtp = "mail.server.com"
$to = "email@email.com"
$from = "email@email.com"
$subject = "Web.configs"
$body = @"
List of Configs Found Attached
List of Configs created below:
$ChangedConfig
"@
#### Now send the email using \> Send-MailMessage
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Attachment $file -Priority high
asked 31 secs ago
Find and replace strings in Config files across an entire stack
Aucun commentaire:
Enregistrer un commentaire