繁体   English   中英

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

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

昨天我在服务器上的某些批​​处理文件上运行了以下脚本,以替换某人的电子邮件地址,因为她不再在公司工作。 在检查文本时,它可以正常工作,并且日志文件也可以正确写入。 但是,现在当我双击批处理文件时,它不再执行。 我看到控制台窗口快速闪烁,但其中似乎没有任何文本。 添加PAUSE语句没有帮助,因为它似乎无法执行文件中的任何文本。

我将文本复制并粘贴到新的批处理文件中,效果很好。 我注意到,PowerShell编辑的文件为6KB,新的复制粘贴的文件为3KB,因此很明显,脚本对该文件做了一些意外的事情。 复制和粘贴每个文件显然会破坏使用脚本批量处理事物的目的。 有什么想法我要去哪里吗?

我一直在开发计算机上运行脚本,并且对网络上的所有内容都拥有完全的管理员权限。

如果有问题,我们将在服务器上运行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默认为Unicode编码(这就是文件大小加倍的原因;每个字符为16位)。 您可以使用Out-File -Encoding ASCII或“ Set-Content (默认为ASCII)。

暂无
暂无

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

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