2

Yesterday I ran the following script on some batch files on our server to replace an individual's email address as she is no longer with the company. When examining the text it worked perfectly and the log file wrote correctly as well. However, now the batch file no longer executes when I double click on it. I see a quick flash of the console window but there does not appear to be any text in it. Adding PAUSE statements is not helpful as it does not seem to execute any of the text in the file.

I copied and pasted the text to a new batch file and it works fine. I noticed that the powershell-edited file is 6KB and the new copied-and-pasted file is 3KB so clearly the script has done something unexpected to the file. Copying and pasting each file obviously defeats the purpose of using a script to batch process things. Any ideas where I'm going wrong?

I've been running the script from my development machine and I have full administrator permissions to everything on our network.

If it matters we are running Windows Server 2008 R2 Enterprise on the server.

# Finds string in batch files recursively and replaces text with other text
# 

#get command line arguments
param (
    [string]$Text = $( Read-Host "Input the text to search for"),
    [string]$Replacement = $( Read-Host "Input the replacement text")
 )

$Today=get-date -format yyyymmdd
$Results = "{server name}\c$\Batch Jobs\Find Text In Batch Jobs\ReplaceTextInBatchJobs-" + $Text + "-" + $Today + ".txt"
$Path = "{server name}\c$\Batch Jobs"

# get all the files in $Path that end in ".bat".
Get-ChildItem $Path -Filter "*.bat" -Recurse | 
   Where-Object { $_.Attributes -ne "Directory"} | 
      ForEach-Object { 
        #Find whether there is a matching string
         If (Get-Content $_.FullName | Select-String -Pattern $Text) {
            #Replace the text in this file
            (Get-Content $_.FullName) | Foreach-Object {$_ -replace $Text, $Replacement} | 
                    Out-File $_.FullName #write the new results back to the file

            #write the file name to a log file
            $_.FullName >> $Results
         }
}
2
  • 1
    Just thought I'd point out that your $Today variable could be populating the date incorrectly. 'yyyymmdd' would turn out looking like this: 20153330 (for the record it's June 30, 2015 at 2:33pm at time of posting) This is because when formatting dates, lowercase 'mm' will insert the minutes. Uppercase 'MM' is for the month. Commented Jul 30, 2015 at 19:33
  • Oops, thanks for pointing that out. Not critical but could certainly be confusing later! Commented Jul 31, 2015 at 19:26

1 Answer 1

2

Out-File defaults to Unicode encoding (which is why the file doubles in size; each character is 16 bits). You can either use Out-File -Encoding ASCII or Set-Content which defaults to ASCII.

Sign up to request clarification or add additional context in comments.

1 Comment

So simple... I knew I must be missing something fairly key to the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.