如何纠正PowerShell脚本/ WebAdministration foreach循环的Write-Host结果?

时间:2019-06-12 00:29:54

标签: powershell web-administration

我想将4个foreach循环结果显示为:

Server  
   IIS Site:  
   App Pool:  
   Service:  

代替:

Server  
  IIS Site:  
Server  
  App Pool:  
Server  
  Service:  

代码:

foreach ($server in $servers)  
{  
    foreach ($IISsite in $IISsites) { }  
    foreach ($appPool in $appPools) { }  
    foreach ($service in $services) { }  
}  
CheckSitesAppPoolsServices -servers "SERVER1" -IISsites ("Default Web Site")  
CheckSitesAppPoolsServices -servers "SERVER1" -appPools ("DefaultAppPool")  
CheckSitesAppPoolsServices -servers "SERVER1" -services ("User Profile Service", "App Readiness")  

实际结果:

Review Sites, App Pools and Service on SERVER1
Default Web Site.......................................  Started

Review Sites, App Pools and Service on SERVER1
DefaultAppPool.........................................  Started

Review Sites, App Pools and Service on SERVER1
User Profile Service...................................  Running  
App Readiness..........................................  Stopped

我希望结果显示如下:

Review Sites, App Pools and Service on SERVER1

Site:     Default Web Site.......................................  Started  
App Pool: DefaultAppPool.........................................  Started  
Service:  User Profile Service...................................  Running  
Service:  App Readiness..........................................  Stopped

1 个答案:

答案 0 :(得分:0)

尝试一下:

function Get-SiteAppPoolServiceStatus
{
    param (
        [Parameter(Mandatory = $true)]
        [String[]] $Servers,

        [Parameter(Mandatory = $false)]
        [String[]] $IISSites = @(),

        [Parameter(Mandatory = $false)]
        [String[]] $AppPools = @(),

        [Parameter(Mandatory = $false)]
        [String[]] $Services = @()
    )                

    $status = @()

    foreach ($server in $Servers)
    {
        foreach ($IISSite in $IISSites) 
        { 
            $status += [PSCustomObject]@{ "Server" = $server; "Type" = "Site"; "Name" = $IISSite; "Status" = "Started" }
        }  

        foreach ($appPool in $AppPools)  
        { 
            $status += [PSCustomObject]@{ "Server" = $server; "Type" = "App Pool"; "Name" = $appPool; "Status" = "Started" }
        }  

        foreach ($service in $Services)
        { 
            $status += [PSCustomObject]@{ "Server" = $server; "Type" = "Service"; "Name" = $service; "Status" = "Started" }
        }  
    }

    Write-Output $status
}

Get-SiteAppPoolServiceStatus -Servers "SERVER1" -IISSites "Default Web Site"
Get-SiteAppPoolServiceStatus -Servers "SERVER1" -AppPools "DefaultAppPool"
Get-SiteAppPoolServiceStatus -Servers "SERVER1" -Services @("User Profile Service", "App Readiness") 
Get-SiteAppPoolServiceStatus -Servers "SERVER2" -IISSites "Web Site" -AppPools "AppPool" -Services @("Test1", "Test2") 

这将导致以下输出:

Server  Type     Name                 Status
------  ----     ----                 ------
SERVER1 Site     Default Web Site     Started
SERVER1 App Pool DefaultAppPool       Started
SERVER1 Service  User Profile Service Started
SERVER1 Service  App Readiness        Started
SERVER2 Site     Web Site             Started
SERVER2 App Pool AppPool              Started
SERVER2 Service  Test1                Started
SERVER2 Service  Test2                Started

您还可以使用函数输出来对其进行不同的格式化。