需要我的PowerShell日志文件以表格格式

时间:2016-01-11 14:32:59

标签: powershell logfile

我最近在本网站上有人的帮助下完成了我的剧本(Matt)再次感谢!

我现在需要以某种方式将日志文件转换为表格格式,我不知道如何使用当前的脚本设置,任何想法来实现它?

Write-Host Report generated at (Get-date)

write-host("Lower Environments Status Check");

# Preprocessing Items
$msg = ""
$array = get-content C:\LowerEnvChecklist\appurls.txt
$log = "C:\LowerEnvChecklist\lowerenvironmentslog.txt"
$errorTexts = "error has occurred","Oops","Unable to display widget data","unexpected error occurred","temporarily unavailable","there was a problem"
$regex = ($errorTexts | ForEach-Object{[regex]::Escape($_)}) -join "|"


write-host("Checking appurls.txt...One moment please.");

("`n---------------------------------------------------------------------------") | out-file $log -Append

Get-Date | Out-File $log -Append

("`n***Checking Links***") | out-file $log -Append
("`n") | out-file $log -Append


# Loop through each element of the array.
ForEach($target in $array){

    # Erase results for the next pass in case of error. 
    $result, $response, $stream, $page = $null

    # Navigate to site urls
    $result = [System.Net.WebRequest]::Create($target)
    $response = $result.GetResponse()
    $stream = [System.IO.StreamReader]$response.GetResponseStream()
    $page = $stream.ReadToEnd()

    # To ensure login/authentication pages that give a 403 response pages still show as online
    If($response.StatusCode -eq 403){
                $msg = " $target -----> is ONLINE!"}

    # Determine if the status code 200 pages are truly up based on the information above.             
    If($response.StatusCode -eq 200){

    # While the page might have rendered need to determine there are no errors present.
    If($page -notmatch $regex){
            $msg = " $target -----> is ONLINE!"
        } else {
            $msg = " $target -----> may be DOWN, please check!"
        }
    } else {
        $msg = " $target -----> may be DOWN, please check!"
    }

    # Log Results.
    $msg | Out-File $log -Append -width 120
    write-host $msg

    # Close the response.
    $response.Close()
}

# Write completion to logfile.
("`n") | out-file $log -Append
("`n***Lower Environments Checklist Completed***") | out-file $log -Append

# Write completion to host.
write-host("Lower Environments Checklist Completed");

# Open logfile once script is complete.
Invoke-Item C:\LowerEnvChecklist\lowerenvironmentslog.txt

2 个答案:

答案 0 :(得分:0)

如果您只想在脚本中查看它,可以在日志文件上执行Out-GridView。这将打开一个新窗口,其中包含日志文件中看起来像表的数据视图。根据您的格式,您可能需要添加额外的项目,例如人类可读的标题。

答案 1 :(得分:0)

用结构化输出润湿你的哨子我选择向你展示基于CSV的解决方案。无论哪种方式,所有途径都需要对象。我们在这里做的是创建一个自定义对象,我们在脚本进行时填充该对象。每个传递都会向管道发送详细信息。使用管道,我们可以使用Export-CSV收集一个漂亮文件中的所有数据。甚至可以进行过滤。

write-host("Lower Environments Status Check");

# Preprocessing Items
$array = Get-Content C:\LowerEnvChecklist\appurls.txt
$log = "C:\LowerEnvChecklist\lowerenvironmentslog.csv"
$errorTexts = "error has occurred","Oops","Unable to display widget data","unexpected error occurred","temporarily unavailable","there was a problem"
$regex = ($errorTexts | ForEach-Object{[regex]::Escape($_)}) -join "|"

# Loop through each element of the array. Use the pipeline to make output easier
$array | ForEach-Object{
    # Keep the variable $target so it is not lost in scopes. Build the object to be completed as we go.
    $target = [pscustomobject][ordered]@{
        URL = $_
        Status = ""
        Detail = "N/A"
        Timestamp = Get-Date
    }

    # Erase results for the next pass in case of error. 
    $result, $response, $stream, $page = $null

    # Navigate to site urls. If we cant access the site set a flag to mark the site as down.
    $result = [System.Net.WebRequest]::Create($target.URL)
    $response = try{$result.GetResponse()}catch{$null}

    switch([int]$response.StatusCode){
        403{
            $target.Status = "OK"
            $target.Detail = "403"
        }
        200{
            # Get page content to confirm up status
            $stream = [System.IO.StreamReader]$response.GetResponseStream()
            $page = $stream.ReadToEnd()

            # While the page might have rendered need to determine there are no errors present.
            If($page -notmatch $regex){
                $target.Status = "OK"
            } else {
                $target.Status = "DOWN"
                $target.Detail = "Pattern"
            }
        }
        default{
            $target.Status = "DOWN"
        }
    }

    # Send the object down the pipeline
    $target

    # Close the response. The object might not exist so check before we call the methods. 
    if($response){$response.Close()}
    if($stream){$stream.Close()}
} | Export-CSV -Path $log -NoTypeInformation

# Write completion to host.
write-host("Lower Environments Checklist Completed");

# Open logfile once script is complete.
Invoke-Item $log

我冒昧地在您的请求中添加了另一个名为Detail的列,它可以添加上下文。不确定你想要的日期,但如果你有足够的URL和处理时间,那么我想它可能是有用的。另外,为了减少if逻辑,我添加了switch语句。如果您对其他状态代码做出反应,这将更有用。还是,好的事情要知道。

示例输出

URL                      Status Detail  Timestamp            
---                      ------ ------  ---------            
https://7fweb            DOWN   N/A     1/11/2016 12:18:16 PM
http://www.google.ca     OK     N/A     1/11/2016 12:18:16 PM
http://www.microsoft.com DOWN   Pattern 1/11/2016 12:18:16 PM

我添加了" windows"到$errorTexts以触发microsoft.com的模式匹配