使用PowerShell替换文本文件中的字符串

Ana*_*hah 8 powershell powershell-2.0

我在服务器上的某个位置有一个文本文件.该文件包含字符串值和时间戳.我想使用PowerShell将此特定行替换为最新的时间戳.

以下是记事本文件的数据:

Test Service|05-09-2017 01-09-41

这里05-09-2017 01-09-41是MM-dd-yyyy HH-mm-ss,我想用最新的时间戳替换这一行.

所以,如果两分钟之后应该是

Test Service|05-09-2017 01-11-41

这是我在做的事情:

$ReplaceString = "$ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')"
Get-Content Path -Replace($ServiceName + "|" + $StringLastRunTime,$ReplaceString) | Set-Content Path
Run Code Online (Sandbox Code Playgroud)

但它不会产生预期的产出.

UPDATE

很抱歉没有那么明确的要求

我想使用PowerShell脚本监视服务器上安装的关键服务.我在一个文本文件中列出了要监视的所有服务,并在脚本中提到了该路径.此脚本将每5分钟执行一次,如果未安装服务或服务已停止,则会生成电子邮件通知.为防止电子邮件泛滥,我正在考虑使用文本文件中的服务名称更新时间戳.因此,如果我发现一个服务处于停止模式或未安装,我将发送通知并更新上次发送时间的时间戳.我有一个重发间隔,每五分钟检查一次,如果该间隔通过,我必须再次发送通知,我想更新时间戳.

代码(我删除了电子邮件发送部分,使其不那么复杂):

$ServicesList = Get-Content E:\ServiceReports\Services.txt
$ResendEmailInterval = 10 #In Minutes

foreach ($Service in $ServicesList) {
    $ServiceName = $Service
    $Service = Get-Service -Display $Service -ErrorAction SilentlyContinue
    $serviceDisplayName = $Service.displayname
    if (-Not $Service) {
        #Service is not installed. So need to send an email alert and update the timestamp.
        Write-Host $ServiceName.IndexOf("|")
        if ($ServiceName.IndexOf("|") -gt 0) {
            $ServiceName,$StringLastRunTime = $ServiceName.Split('|')
            $LastRunTime = [datetime]::ParseExact($StringLastRunTime, "MM-dd-yyyy HH-mm-ss", $null)
            Write-Host $ServiceName
            Write-Host $LastRunTime
            Write-Host (Get-Date).AddMinutes($ResendEmailInterval)
            if ($LastRunTime -lt (Get-Date).AddMinutes($ResendEmailInterval)) {
                Write-Host "time to run"
                $ReplaceString = $ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')
                Write-Host $ServiceName"|"$StringLastRunTime
                Write-Host $ReplaceString
                #$ServiceName = "Test Service";
                $ReplaceString = "{0}|{1}" -f $ServiceName, (Get-Date).ToString('MM-dd-yyyy HH-mm-ss');
                (Get-Content -Path $ServicesListPath) -replace ('([^|]+).*', $ReplaceString) | Set-Content $ServicesListPath;
                $ServicesList -replace ($ServiceName + "|" + $StringLastRunTime, $ReplaceString) | Set-Content $ServicesListPath
            }
        } else {
            $ReplaceString = $ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')
            $ServicesList -replace ($ServiceName, $ReplaceString) | Set-Content $ServicesListPath
            $i = $i+1;
        }
    } else {
        if ($Service.Status -eq "Stopped") {
            Write-Host "Service is stopped"
            #Time to send email and update the time stamp
            $i = $i+1;
        } else {
            #Write-Host "Service is running. Nothing to do."
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ndl 9

你可以用正则表达式做到这一点:

$filePath = 'yourPath'
(Get-Content $filePath -raw) -replace '([^|]+).*', ('$1|{0:MM-dd-yyyy HH-mm-ss}' -f (Get-Date)) |
    Set-Content $filePath
Run Code Online (Sandbox Code Playgroud)

正则表达式解释:

在此输入图像描述


Set*_*eth 1

看看您提供的代码,在不知道实际输出是什么的情况下,我希望您$ReplaceString是错误的。你的报价不合算。这应该适用于它:

$ReplaceString = $ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')
Run Code Online (Sandbox Code Playgroud)