Second For-Each不返回任何数据

时间:2017-10-25 17:30:25

标签: powershell foreach

我有一个脚本,它返回数据后维护窗口,服务正在ping,上次重启时间,以及服务是否正在运行。我的第二个for-each在我的脚本中只返回空白时没有返回任何数据,下面是我的脚本。

#### Provide the computer name in $computername variable 

$ServerName = (Get-Content -Path "\\Server\d$\Data\Admin\Scripts\MW 
scripts\Primary.txt")

$ServerNameB = (Get-Content -Path "\\Server\d$\Data\Admin\Scripts\MW 
scripts\Primarylessalt.txt")

##### Script Starts Here ######  


foreach ($Server in $ServerName) { 

    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {  

        write-Host "$Server is alive and Pinging " -ForegroundColor Green 

                } else 

                { Write-Warning "$Server is dead and not pinging" 

                }     

} 

foreach ($Server in $ServerName) { (Get-Service -Name "Print Spooler" -Cn 
$Server) | Select name,status,machinename}

foreach ($Server in $ServerNameB){Get-CimInstance -Cn $Server -ClassName 
win32_operatingsystem | select csname,lastbootuptime}



Format-Table -AutoSize
Write-Host "Script is complete" -ForegroundColor Cyan

Read-Host "Press Enter to Exit"




########## end of script #######################

1 个答案:

答案 0 :(得分:0)

具有不同列的多个表往往会混淆输出解析器。您可以强制它使用Format-Table

刷新结果
foreach ($Server in $ServerName) { (Get-Service -Name "Print Spooler" -Cn 
$Server) | Select name,status,machinename  | Format-Table -AutoSize}

foreach ($Server in $ServerNameB){Get-CimInstance -Cn $Server -ClassName 
win32_operatingsystem | select csname,lastbootuptime  | Format-Table -AutoSize}