输出缺少信息

时间:2016-06-21 13:30:48

标签: xml powershell

我有一个脚本,它读取xml文件,然后ping xml文件中列出的站点并输出到文本文件。哪个工作得很好,除了它只显示ping的最后一个站点而不是所有站点。 xml文件:

<Servers>
    <Server id="1">
        <name>server1</name>
        <cfusion>www.cnn.com</cfusion>
        <dotnet>www.msn.com</dotnet>
    </Server>
    <Server id="2">
        <name>server2</name>
        <cfusion>www.yahoo.com</cfusion>
        <dotnet>www.google.com</dotnet>
    </Server>
    <Server id="3">
        <name>server3</name>
        <cfusion>www.wwe.com</cfusion>
        <dotnet>www.nfl.com</dotnet>
    </Server>
</Servers>

脚本:

[xml]$servers = Get-Content c:\cfusion.xml
$collection = $()

foreach($server in $servers.Servers.Server)  {
    $status = @{ "Cold Fusion" = $server.cfusion; "Dot Net" = $server.dotnet; "Server Name" = $server.name; }

    if (Test-Connection $server.name -Count 1) {
        $status["Status"] = "Online"
    } else {
        $status["Status"] = "Offline"
    }

    if (Test-Connection $server.cfusion -Count 1) {
        $status["Cold Fusion"] = "Up"
    } else {
        $status["Cold Fusion"] = "Down"
    }

    if (Test-Connection $server.dotnet -Count 1) {
        $status["Dot net"] = "Up"
    } else {
        $status["Dot Net"] = "Down"
    }

    New-Object -TypeName PSObject -Property $status -OutVariable serverstatus |
        Format-table -Property "Server Name", "Status", "Cold Fusion", "Dot Net" -AutoSize |
        Out-File c:servstatus.txt
}

$collection

2 个答案:

答案 0 :(得分:0)

脚本有一些错误,可能来自格式化/复制/粘贴。如果您使用-Append上的Out-File参数,它可能会按预期工作。

答案 1 :(得分:0)

尝试更改:

out-file c:servstatus.txt

要:

out-file c:servstatus.txt -Append