Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Powershell:WMI Ping已通过管道传输到变量_Loops_Powershell_Wmi_Ping - Fatal编程技术网

Loops Powershell:WMI Ping已通过管道传输到变量

Loops Powershell:WMI Ping已通过管道传输到变量,loops,powershell,wmi,ping,Loops,Powershell,Wmi,Ping,我在让我的PowerShell脚本按我所希望的方式工作时遇到了一些困难,在经历了许多诡异的扑克游戏之后,我终于来到了这里 我的总体目标相当简单,不幸的是,我有点像一个PowerShell noob! 我正在努力确定我们庄园中所有系统的名称、制造商和型号,而不必四处走动,盯着大量的锡 我完全是基于我糟糕的脚本知识来构建以下内容的,我遇到了一个障碍 我的想法是将DNS/IP信息从CSV传递到一个变量中,然后我可以使用该变量根据Ping结果执行WMI查询。 假Ping响应=不查询 True Ping响

我在让我的PowerShell脚本按我所希望的方式工作时遇到了一些困难,在经历了许多诡异的扑克游戏之后,我终于来到了这里

我的总体目标相当简单,不幸的是,我有点像一个PowerShell noob! 我正在努力确定我们庄园中所有系统的名称、制造商和型号,而不必四处走动,盯着大量的锡

我完全是基于我糟糕的脚本知识来构建以下内容的,我遇到了一个障碍

我的想法是将DNS/IP信息从CSV传递到一个变量中,然后我可以使用该变量根据Ping结果执行WMI查询。 假Ping响应=不查询 True 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"
     }
}

最近发现了以下代码:
$results |导出Csv-路径“C:\Csv\results.Csv”-编码UTF8-分隔符“;”-notype信息