日志文件的字符串变量丢失返回字符

时间:2014-06-17 16:59:20

标签: powershell-v2.0 traceroute

我正在使用powershell执行tracert命令并将结果放入变量中。

$TraceResult = tracert $server 

当使用write-host时,输出在屏幕上看起来很好,每个都跳到新行上。

Tracing route to fhotmail.com [65.55.39.10]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  BTHomeHub.home [192.168.50.1] 
  2     *        *        *     Request timed out.
  3     *        *        *     Request timed out.
  4     *        *        *     Request timed out.
  5     *        *        *     Request timed out.

当我使用add-content将变量输出到文件时,返回的字符会丢失,格式看起来很糟糕,因为它在一行上:

17/06/2014  14:48:26     Tracing route to fhotmail.com [64.4.6.100] over a maximum of 30 hops:    1    <1 ms    <1 ms    <1 ms  BTHomeHub.home [192.168.50.1]    2     *        *        *     Request timed out.   3     *        *        *     Request timed out.   4     *        *        *     Request timed out.   5     *        *        *     Request timed out.   6     *        *        *     Request timed out.   7     *        *        *     Request timed out.   8     *        *        *     Request timed out.   9     *        *        *     Request timed out.  10     *        *        *     Request timed out.  11     *        *        *     Request timed out.  12     *        *        *     Request timed out.  13     *        *        *     Request timed out.  14     *        *        *     Request timed out.  15     *        *        *     Request timed out.  16     *        *        *     Request timed out.  17     *        *        *     Request timed out.  18     *        *        *     Request timed out.  19     *        *        *     Request timed out.  20     *        *        *     Request timed out.  21     *        *        *     Request timed out.  22     *        *        *     Request timed out.  23     *        *        *     Request timed out.  24     *        *        *     Request timed out.  25     *        *        *     Request timed out.  26     *        *        *     Request timed out.  27     *        *        *     Request timed out.  28     *        *        *     Request timed out.  29     *        *        *     Request timed out.  30     *        *        *     Request timed out.  Trace complete.

关于我哪里出错了?

        $servers = "localhost","google.com","fhotmail.com"
    $Logfile = "c:\temp\$(Get-Content env:computername).log"  # Sets the logfile as c:\temp\[COMPUTERNAME].log

    # Countdown function with progress bar
    Function Start-Countdown 
    {   
        Param(
            [Int32]$Seconds = 10,
            [string]$Message = "Pausing for 10 seconds..."
        )
        ForEach ($Count in (1..$Seconds))
        {   Write-Progress -Id 1 -Activity $Message -Status "Waiting for $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)
            Start-Sleep -Seconds 1
        }
        Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
    }

    # Log Write Function to log a message with date and time (tab delimited)
    Function LogWrite
    {
       Param ([string]$logstring)

       Add-content $Logfile -value "$(get-date -UFormat '%d/%m/%Y') `t$(get-date -UFormat '%T') `t$logstring"
    }

    # Looping script to run ping tests, then countdown for a period, before repeating
    while ($true) {

        # Ping servers and perform tracert if not responding to ping
        foreach ( $server in $servers ) {
                write-Host "Pinging: $server" -ForegroundColor Green
                if ((test-Connection -ComputerName $server -Count 2 -Quiet) -eq $true ) { 
                    # Ping response received.   
                    write-Host "Response Received Sucessfully [$server].`n" -ForegroundColor Green
                    } 
                else { 
                    # Ping response failed, next perform trace route.
                    write-Host "Response FAILED [$server]." -ForegroundColor RED 
                    LogWrite "Ping Response FAILED [$server]."
                    Write-Host "Traceroute: $server - $(get-date -UFormat '%d/%m/%Y - %T')" -ForegroundColor Yellow
                    $TraceResult = tracert $server 
                    LogWrite $TraceResult
                    Write-Host "Traceroute Completed [$server]`n" -ForegroundColor Yellow
                    }                   
        }


        # Pause before repeating
        Start-Countdown -Seconds 60 -Message "Pausing script before restarting tests.  Use CTRL-C to Terminate."

    }

1 个答案:

答案 0 :(得分:0)

PowerShell不会将tracert的输出作为单个字符串处理,而是一个字符串数组(每行一个),并且它试图将它们全部强制转换为单个字符串(基本上将它们连接起来)

假设你对这样的输出没问题:

17/06/2014  16:54:07    
Tracing route to SERVERNAME [IP]
over a maximum of 30 hops:

  1     1 ms    <1 ms    <1 ms  HOP1 
  2     1 ms    <1 ms    <1 ms  IP 

Trace complete.

你需要这样做:

$TraceResult = tracert $server 
LogWrite ($TraceResult -join "`r`n")

这会将$TraceResult数组的所有元素连接成一个字符串,其中 CRLF 作为EOL标记。