简体   繁体   English

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

[英]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: 我想将4个foreach循环结果显示为:

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. 您还可以使用函数输出以不同方式对其进行格式化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Powershell DSC 中查看脚本资源的 Write-Host 输出? - How can I see the Write-Host output of a script resource in Powershell DSC? Powershell foreach写主机作为单独的记录 - Powershell foreach write-host as individual records 如何在创建的 powershell shell 中包含 Write-Host 命令的代码中执行脚本? - How can I execute scripts in a code created powershell shell that has Write-Host commands in it? PowerShell:如何捕获(或抑制)写主机 - PowerShell: how to capture (or suppress) write-host PowerShell Forms。如果我不能调用 Function 或使用 Write-output,怎么会出现在我的表单关闭事件中,但如果我使用 Write-Host,它会起作用 - PowerShell Forms. How come in my Form Closing event If I can't call a Function or use Write-output, but it works if I use Write-Host PowerShell写主机-NoNewLine - PowerShell Write-Host -NoNewLine 如何在 powershell 中使用 Write-Host 将 output 和结果都获取到变量? - How to get both the output and results to a variable using Write-Host in powershell? Java 使用 function 调用 Powershell 脚本并且不将写入主机返回到 Z93F725A07423FE1DC846F - Java calling Powershell script with function and not returning Write-Host to java Powershell 脚本输出到变量 - 捕获 Write-Host 输出 - Powershell Script output to variable - capture Write-Host output 在此Powershell脚本中进行过滤时,如何用export-csv替换write-host - How to replace write-host with export-csv while filtering in this powershell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM