在ForEach中使用pscustomobject时无法获取命令输出

时间:2019-04-25 06:25:30

标签: powershell powershell-v3.0 powershell-v4.0

我正在尝试从服务列表中获取服务状态。我正在从列表中为每个服务在pscustomobject中捕获结果。

Write-Host "$($_.DisplayName) : ($($_.Name))  $($_.Status) : Startup Type : $($_.StartType)"

我没有从上面的代码中得到以下行的期望值:

 : ()   : Startup Type : 
 : ()   : Startup Type : 
 : ()   : Startup Type :

我不希望以上行进入$ PCServiceDetails。我得到的输出是:

Sub delete()
    tamano = Range("J2") ' Value into J2
    ifrom = 7 ' where you want to delete
    'Borramos las celdas
    'Delete column A , B and C
    For i = ifrom To tamano
    Range("A" & i).Value = ""
    Range("B" & i).Value = ""
    Range("C" & i).Value = ""
    Next i
End Sub

请咨询。谢谢

2 个答案:

答案 0 :(得分:0)

您不能像这样将管道对象传递给Write-Hostwrite-output

这是双重try / catch的完整逻辑,因此在一项服务失败的情况下它不会终止。以及您在管道中面临的问题。

try
{
$PCServices = @("AJRouter",
"ALG",
"bthserv",
"CertPropSvc",
"Dnscache",
"MapsBroker",
"lfsvc",
"HomeGroupListener",
"HomeGroupProvider",
"vmickvpexchange",
"vmicguestinterface",
"vmicshutdown",
"vmicheartbeat",
"vmicrdv",
"vmictimesync",
"vmicvss",
"SharedAccess",
"iphlpsvc");    
Write-Host "Checking PC Service Status"

        $PCServiceDetails = ForEach($PCSvc in $PCServices)
        {
            try
            {
                $service = Get-Service -Name $PCSvc -ErrorAction Stop
                Write-Host "$($service.DisplayName) : ($($service.Name))  $($service.Status) : Startup Type : $($service.StartType)"
            }
            catch
            {
            "Issue in fetching information for the service: $PCSvc"
            $_.Exception.Message
            }
        }
}
catch
{
 Write-Host "Unable to get service details: $($PCServiceDetails.Name)/$($PCServiceDetails.DisplayName) : $($_.Exception)"
}

希望有帮助。

答案 1 :(得分:0)

RanadipDutta指出,Write-Host不接受管道输入。我忘记了。 [脸红]

这是我对你的问题的看法。它将try/catch放在应有的位置-每次调用Get-Service时,而不是整个循环中。

$PC_ServiceList = @(
    'bits'
    'spooler'
    'w32time'
    'zzz_This_Better_Not_Be_There'
    )

Write-Host 'Checking PC Service Status ...'
Write-Host ''
$PC_ServiceDetails = ForEach ($PCSL_Item in $PC_ServiceList)
    {
    try
        {
        $ServiceInfo = Get-Service -Name $PCSL_Item -ErrorAction Stop

        Write-Host ('{0} : {1}  {2} : Startup Type : {3}' -f $ServiceInfo.DisplayName,
            $ServiceInfo.Name, $ServiceInfo.Status, $ServiceInfo.StartType)

        $ServiceInfo
        }
        catch
        {
        Write-Host ''
        Write-Host ('Unable to get service details: {0} : {1}' -f $PCSL_Item, $_)
        }
    }

$PC_ServiceDetails

输出...

Checking PC Service Status ...

Background Intelligent Transfer Service : bits  Running : Startup Type : Manual
Print Spooler : spooler  Running : Startup Type : Automatic
Windows Time : w32time  Stopped : Startup Type : Manual

Unable to get service details: zzz_This_Better_Not_Be_There : Cannot find any service with service name 'zzz_This_B
etter_Not_Be_There'.

Status   Name               DisplayName
------   ----               -----------
Running  bits               Background Intelligent Transfer Ser...
Running  spooler            Print Spooler
Stopped  w32time            Windows Time