Set-Content blanking files - powershell

I have the following script that is supposed to replace two keywords in a few SQL and batch scripts and then return them to the original:
# Modify TRIS 5 Scripts to account for database names used
$configFiles = Get-ChildItem (Join-Path $PSScriptRoot "\tris5scripts\") -Rec
foreach ($file in $configFiles) {
(Get-Content $file.PSPath) |
ForEach-Object { $_ -replace "TRISDemo4113", $databaseName } |
Set-Content $file.PSPath
ForEach-Object { $_ -replace "internal", $tris5Name } |
Set-Content $file.PSPath
}
cmd.exe (Join-Path $PSScriptRoot "\tris5scripts\execAll.cmd")
cmd.exe (Join-Path $PSScriptRoot "\tris5scripts\Prepare Dashboard Statistics.cmd")
if ($databaseName -ne "TRISDemo4113" -or $tris5Name -ne "internal") {
$configFiles = Get-ChildItem (Join-Path $PSScriptRoot "tris5scripts") -Rec
foreach ($file in $configFiles) {
(Get-Content $file.PSPath) |
ForEach-Object { $_ -replace $databaseName, "TRISDemo4113" } |
Set-Content $file.PSPath
ForEach-Object { $_ -replace $tris5Name, "internal" } |
Set-Content $file.PSPath
}
}
What is happening though is that the script makes every file in this folder blank. I'm not sure how as I have used this exact code snippet in another script and it works fine. It seems to get to the two cmd.exe calls and does nothing because every file is blank now.
All I am trying to do is change those two values in the files:
"TRIS4133" → $databaseName
"internal" → $tris5Name

Related

Replace the text for all files in a Directory

I have written the below conditional script to go through the files in the directory and replace the one text in all files only if file contains the word as 'Health'
cd -Path "\\shlhfilprd08\Direct Credits\Temp2"
ForEach ($file in (Get-ChildItem -Path "\\shlhfilprd08\Direct Credits\Temp2"))
{
$filecontent = Get-Content -path $file -First 1
if($filecontent -like '*Health*'){$filecontent = $filecontent -replace 'TEACHERF','UniHlth '}
Set-Content $file.PSpath -Value $filecontent
}
I come across with two issues such as
If the ($filecontent -like 'Health'), it is replacing the word in first raw and deleting other rows along with replace.I do not want that to happen
I'm getting set-content to path is denied error message for file content does not contain the Health text
Can you try with this
cd -Path "\\shlhfilprd08\Direct Credits\Temp2"
$configFiles = Get-ChildItem . *.config -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "TEACHERF", "UniHlth " } |
Set-Content $file.PSPath
}
I would try this; it worked for me in a little file
(make a small copy of a few data into a new folder and test it there)
$path = "\\shlhfilprd08\Direct Credits\Temp2"
$replace ="TEACHERF" #word to be replaced
$by = "UniHlth " #by this word (change $replace by $by)
gci $path -file | %{
foreach($line in $(Get-content $_.Fullname)){
if($line -like $replace){
$newline = $line.Replace($($replace),$($by))
Set-Content $_.FullName $newline
}
}
}

find and replace special character " in multiple files using powershell

in particular path i need to find ""' and replace it with "' in multiple files
Tried below code but its not working due to special character to be found and replaced
$configFiles = Get-ChildItem . *.ini -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace """'", ""'" } |
Set-Content $file.PSPath
}
You're not escaping your characters properly:
$configFiles = Get-ChildItem -Filter *.ini -Recurse
ForEach ($file in $configFiles)
{
#(Get-Content -Path $file.FullName) -replace "`"{2}'", "`"'" |
Set-Content -Path $file.FullName
}

Showing information after replacement

I use below code to change strings in files:
Set-Location -Path C:\Users\Documents\corporate
foreach ($file in get-ChildItem *.rdl)
{
$_.Replace("Protection", "Converters") | Set-Content $file
$_.Replace("Drives", "Automation") | Set-Content $file
$_.Replace("MACHINES", "Generators") | Set-Content $file
$file.name
}
I want to add information what has changed in individual files.
For example:
file 1 Protection
file 3 Protection, MACHINES
try this way ...
Get-Content -Path "C:\Users\Documents\corporate" -Filter "*.rdl" | ForEach-Object {
$Local:CurrentFileFullName = $_.FullName
((Get-Content -Path $CurrentFileFullName ) -replace "Protection", "Converters" -replace "Drives", "Automation" -replace "MACHINES", "Generators" | Set-Content $CurrentFileFullName -Force)
}

PowerShell - Copy-item with -passThru to post process files

Newbie stuck doing the basics.
I'm trying to copy files from A to B & then replace text within each file copied. Pretty basic - but it fails What am I doing wrong? Any assistance would be welcome - please. nb I believe I need to use copy-item, alternatives okay as long as same result.
$files = Copy-Item -Path "C:\from" -Filter *.* -Recurse -Destination "C:\to" -Force -PassThru
foreach ($file in $files) {
Get-Item $file |
Where-Object {-not $_.PsIsContainer} |
(Get-Content .) |
Foreach-Object {
$_ -replace ( "maskkey", $maskvalue} |
Set-Content $file
}
}
You need to move the get-content part within the foreach-object script block, and use $_ instead of .
So, something like this:
foreach($file in $files){
Get-Item $file | Where-Object {-not ($_.PsIsContainer)} | Foreach-Object{
$content = Get-Content $_
$newContent = $content.replace("maskkey", $maskvalue)
Set-Content -Value $newContent -Path $file
}
}
Note. I haven't actually tested this code, but you should at least have the basic structure right.

Replace multiple strings in a file using powershell

I am using following coe to replace the string
$folders=Get-ChildItem -Path "C:\temp\Database Scripts"
foreach($folder in $folders)
{
Write-Host $folder
$spath=[string]::Concat("C:\temp\Database Scripts\", $folder)
$subfolders=Get-ChildItem $spath
foreach($subfolder in $subfolders )
{
if($subfolder -match "Running Scripts")
{
$subfolerpath=[string]::Concat($spath,"\",$subfolder,"\*")
$files =get-childitem -Path $subfolerpath -include "AVEVAScripts*"
if($files -ne $null)
{
foreach( $file in $files)
{
Write-Host $file;
(Get-Content $file) | ForEach-Object {$_ -replace "DATABASE_USER","fhghjgj" `
-replace "DATABASE_PASSWORD", "DFGHFHJGJH" } |Set-Content $file
}
}
}
}
}
But ending up with following error.
Set-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
Please help :)
Remove the $x in the end of Set-Content. $x is never declared.
Also, you could simplify it a lot. Ex:
Get-ChildItem -Filter "Running Scripts" -Path "C:\temp\Database Scripts" -Recurse | ForEach-Object {
Get-ChildItem -Path $_.FullName -Filter "AVEVAScripts*" -Recurse | ForEach-Object {
(Get-Content $_.FullName) | ForEach-Object {
$_ -replace "DATABASE_USER","fhghjgj" -replace "DATABASE_PASSWORD", "DFGHFHJGJH"
} | Set-Content $_.FullName
}
}
Or find all files that includes "AVEVAScripts" in it's name, then check if their full path includes "Running Scripts"
Get-ChildItem -Filter "AVEVAScripts*" -Path "C:\temp\Database Scripts" -Recurse |
Where-Object { $_.FullName -like "*Running Scripts*" } |
ForEach-Object {
(Get-Content $_.FullName) | ForEach-Object {
$_ -replace "DATABASE_USER","fhghjgj" -replace "DATABASE_PASSWORD", "DFGHFHJGJH"
} | Set-Content $_.FullName
}