简体   繁体   中英

How can I correct the Write-Host results of my PowerShell script / WebAdministration foreach loops?

I would like to display the 4 foreach loops results as:

Server  
   IIS Site:  
   App Pool:  
   Service:  

Instead of:

Server  
  IIS Site:  
Server  
  App Pool:  
Server  
  Service:  

Code:

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")  

Actual Results:

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

I would like the results to display like this instead:

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

Try this:

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") 

This will result in the following output:

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

You could also use the function output to format it differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM