完成的 Powershell 任务的时间戳

Timestamp of finished Powershell task

我有一个基本脚本,它将关闭 Windows 服务并生成有关其关闭过程的报告。我还想在我的输出变量 ($table) 中包含另外两列,它们将是时间戳值,即关闭任务何时启动和何时完成。我不知道如何将其应用到我的报告中。

$processlist = @('SQLTELEMETRY$TESTDB', 'MSSQL$TESTDB', 'SQLWRITER')
$get = ''
$table = @{ }
$failed = 0


foreach ($proc in $processlist) {
    stop-service -name $proc -force
}

#start-sleep -s 120


foreach ($proc in $processlist) {

    $get = get-service $proc -Erroraction ignore

    if ($get.Status -eq 'Running') {
        $table += @{$proc = 'Running' }
    }
    else {
        $table += @{$proc = 'Stopped' }
    }
}


foreach ($value in $table.GetEnumerator()) {

    if ($value.Value -eq 'Running') {
        $failed += 1
    }
}

if ($failed -gt 0) {
    $err = 'FAILED'
}
else {
    $err = 'SUCCESS'
}

$table.GetEnumerator() | Select-Object -Property Name, Value | export-csv appreport.csv -delimiter ";" -force -notypeinformation

(HTML part here...)

与其将内容添加到哈希表中,我认为构建对象数组并将其写入 CSV 文件会容易得多。

像这样:

$serviceList = 'SQLTELEMETRY$TESTDB', 'MSSQL$TESTDB', 'SQLWRITER'
$maxAttempts = 10

# $result will become an array of PsCustomObjects you can easily pipe to Export-Csv
$result = foreach ($service in $serviceList) {
    $shutStart = Get-Date
    $svc       = Get-Service -Name $service -ErrorAction SilentlyContinue
    if ($svc) {
        for ($attempt = 0; $attempt -lt $maxAttempts; $attempt++) {
            $shutResult = 'Failed'
            Start-Sleep -Milliseconds 100
            $svc | Stop-Service -Force -ErrorAction SilentlyContinue
            # test if the service has stopped. If so exit the loop
            if (($svc | Get-Service).Status -eq 'Stopped') { 
                $shutResult = 'Success'
                break 
            }
        }
        [PsCustomObject]@{
            'ServiceName'        = $svc.Name
            'ServiceDisplayName' = $svc.DisplayName
            'ShutDownStart'      = $shutStart
            'ShutDownEnd'        = Get-Date
            'Result'             = $shutResult
        }
    }
    else {
        [PsCustomObject]@{
            'ServiceName'        = $service
            'ServiceDisplayName' = ''
            'ShutDownStart'      = $shutStart
            'ShutDownEnd'        = Get-Date
            'Result'             = "Failed: Service '$service' could not be found."
        }
    }
}

# output on screen
$result

# output to CSV
$result | Export-Csv 'D:\appreport.csv' -Delimiter ";" -Force -NoTypeInformation

屏幕上的输出将如下所示:

ServiceName        : SQLTELEMETRY$TESTDB
ServiceDisplayName : 
ShutDownStart      : 22-8-2019 16:47:40
ShutDownEnd        : 22-8-2019 16:47:40
Result             : Failed: Service 'SQLTELEMETRY$TESTDB' could not be found.

ServiceName        : MSSQL$TESTDB
ServiceDisplayName : 
ShutDownStart      : 22-8-2019 16:47:40
ShutDownEnd        : 22-8-2019 16:47:40
Result             : Failed: Service 'MSSQL$TESTDB' could not be found.

ServiceName        : SQLWRITER
ServiceDisplayName : SQL Server VSS Writer
ShutDownStart      : 22-8-2019 16:47:38
ShutDownEnd        : 22-8-2019 16:47:39
Result             : Success

希望对您有所帮助

我真的不知道你什么时候想为服务捕获时间戳,但我建议你利用下面的 属性 并将它添加到你认为合适的循环中。

(Get-Process -Name $proc).StartTime

您还可以使用以下属性:

  • 用户处理器时间
  • 总处理时间
  • 退出时间

我希望这能帮助你捕捉到时间。