获取服务结果始终显示最后

时间:2018-01-30 14:58:20

标签: powershell service

我正在尝试编写PowerShell脚本,以便让我的客户在服务器上运行快速运行状况检查,以查看在某些内容无法正常工作时是否存在任何明显错误。我想要做的一件事是检查我们的程序运行所需的服务的状态。我还想确保PowerShell窗口保持打开状态,以便能够查看结果,因此在脚本的最后我添加了一行:

read-host "Press Enter key to close"   

所以我正在与之合作:

#Check status of Services

write-host "Checking status of Product and IIS Services" -BackgroundColor DarkRed
get-service | where {$_.DisplayName -like '*Product*'} | select DisplayName,Status
get-service | where {$_.DisplayName -like '*iis*'} | select DisplayName,Status


write-host "Health check complete"
read-host "Press Enter key to close"

不幸的是,直到最后这似乎才显示服务:

Checking status of Product and IIS Services

Health check complete
Press Enter key to close: 
DisplayName                              Status
-----------                              ------
Product Service 1                       Running
Product Service 2                       Running
Product Service 3                       Running
Product Service 4                       Running
IIS Admin Service                       Running

不幸的是,这失败了目的,因为现在事情不仅没有出现问题,而且PowerShell窗口在显示服务后立即关闭。知道如何按照他们在脚本中列出的顺序显示这些内容吗?

2 个答案:

答案 0 :(得分:2)

这将提供您想要的内容,同时也更准确(不那么模糊)。

Write-Host 'Checking status of Product and IIS Services.' -BackgroundColor DarkRed

Get-Service -DisplayName '*Product*','*iis*' |
    Select-Object -Property DisplayName,Status |
    Out-Host

Write-Host 'Health check complete.'
Pause

答案 1 :(得分:-1)

试试这个

write-host "Checking status of Product and IIS Services" -BackgroundColor DarkRed
$list += get-service | where {$_.DisplayName -like '*iis*'} | select DisplayName,Status
$list += get-service | where {$_.DisplayName -like '*expl*'} | select DisplayName,Status

write-host "Health check complete"
Foreach ($item in $List) {
    Write-Host $Item.displayName - $item.status}
read-host "Press Enter key to close"