2014-05-23 15 views
0

我在讓我的PowerShell腳本按照我喜歡的方式工作並且在此處執行很多jiggery-pokery之後遇到了一些困難。Powershell:WMI Ping通過管道連接到變量

我的總體目標非常簡單,不幸的是我有點像PowerShell noob! 我試圖確定我們產業中所有系統的名稱,製造商和型號,而不必四處盯着大量的錫罐。

我已經構建了以下完全基於我的腳本知識不足,我已經遇到了一個障礙。

我的想法是將CSV/IP信息從CSV傳遞到一個變量,然後我可以依次使用該變量來執行基於Ping結果的WMI查詢。 假Ping反應=不查詢 真平響應=執行WMI查詢

這裏是我這麼遠......

Test-connection -computername 
foreach ($Ping in $Hosts) 
{ 
    test-connection -computername $Ping.IP -count 1 -quiet 
    if ($Ping.StatusCode -eq 0) 
    {Get-WmiObject win32_computersystem -computername $ip.Name | select Name,Manufacturer,Model } out-file c:\CSV\Test1.csv -ea SilentlyContinue} 
    else 
    {write-host $Hosts.Status Offline} 
} 

回答

0

假設你有文件C:\ CSV \主機。與內容CSV描述:

computer1.mydomain.com 
computer2.mydomain.com 

用下面的腳本,你會得到文件C:\ CSV \ Results.csv:

$Hosts = Get-Content "C:\CSV\Hosts.csv" 

foreach ($PingIP in $Hosts) 
{ 

    $alive = Test-Connection -ComputerName "$PingIP" -count 1 -quiet 
    if ($alive) 
    { 
     Get-WmiObject Win32_ComputerSystem -ComputerName "$PingIP" | Select Name, Manufacturer, Model | Out-File "C:\CSV\Results.csv" -ea SilentlyContinue 
    } 
    else 
    { 
     Write-Output "$PingIP offline" 
    } 
} 
+0

最近發現這個代碼:'$ results | Export-Csv -Path「C:\ CSV \ Results.csv」 - 編碼UTF8 -Delimiter';' -NoTypeInformation' –