简体   繁体   English

在批处理文件上运行查找/替换Powershell脚本后,不再执行

[英]Batch file no longer executes after running find/replace Powershell script on it

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. 添加PAUSE语句没有帮助,因为它似乎无法执行文件中的任何文本。

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. 我注意到,PowerShell编辑的文件为6KB,新的复制粘贴的文件为3KB,因此很明显,脚本对该文件做了一些意外的事情。 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. 如果有问题,我们将在服务器上运行Windows Server 2008 R2 Enterprise。

# 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
         }
}

Out-File defaults to Unicode encoding (which is why the file doubles in size; each character is 16 bits). Out-File默认为Unicode编码(这就是文件大小加倍的原因;每个字符为16位)。 You can either use Out-File -Encoding ASCII or Set-Content which defaults to ASCII. 您可以使用Out-File -Encoding ASCII或“ Set-Content (默认为ASCII)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM