[英]Powershell: WMI Ping piped to variable
在使我的PowerShell腳本能夠按我希望的那樣工作時,我遇到了一些困難。
我的總體目標很簡單,不幸的是我有點像PowerShell菜鳥! 我試圖確定我們庄園中所有系統的名稱,制造商和型號,而不必四處張望。
我僅基於對腳本的不良了解而構建了以下內容,但遇到了麻煩。
我的想法是將DNS / IP信息從CSV傳遞到一個變量中,然后我可以使用該變量依次根據Ping結果執行WMI查詢。 假Ping響應=不查詢真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}
}
假設您有文件C:\\ CSV \\ Hosts.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"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.