首页 > 解决方案 > 如何将所有 IIS 站点和应用程序池详细信息导出到 Excel 工作表

问题描述

我编写了一个 PS 脚本,其中会将所有 IIS 站点详细信息导出到 Excel 表,但是当我尝试使用 IIS 应用程序池时,它没有给出所有参数的输出,并且应用程序池的数据在之后显示站点详细信息,但我需要与同一行中的站点数据并行的应用程序池数据,请帮助我解决此问题。

代码

Clear-Host
$computers = Get-Content "C:\TEMP\servers.txt" 


    Invoke-Command -ComputerName $computers -ScriptBlock {
    & {
    Get-Website | Select-Object Name,Id,State,PhysicalPath,
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ‘;’ }} ,
@{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ‘;’ }}
Get-IISAppPool | Select-Object Name,Status,CLRVer,PipeLOneMode,StartMode
}
} | Export-Excel -Path C:\users\$env:username\documents\IIS_sites.xlsx```


**Output**
*name           :Test
id             : 2
state          : Started
physicalPath   : C:\Chadrakanth
Bindings       : http 123.com
attributes     : name=2696Test;id=2;serverAutoStart=False;state=1
PSComputerName : AAA
RunspaceId     : 8ec7998e-03e0-47c7-b96a-ffae96260922

Name           : DefaultAppPool
Status         : 
CLRVer         : 
PipeLOneMode   : 
StartMode      : OnDemand
PSComputerName : AAA
RunspaceId     : 8ec7998e-03e0-47c7-b96a-ffae96260922*

标签: powershell

解决方案


如注释中所述StatusCLRVer或不是可用属性,PipeLOneMode已分别替换为State, 。ManagedRuntimeVersionManagedPipelineMode

此外,由于每个网站都是 AppPool 的成员,因此需要遍历网站并为每个网站找到适当的 AppPool。然后可以使用[PSCustomObject]类型加速器将属性组合成一个对象。

$Computers = Get-Content "C:\TEMP\servers.txt" 

Invoke-Command -ComputerName $Computers -ScriptBlock {
    # Changed to newer IISAdministration Module to match Get-IISAppPool
    $Websites = Get-IISSite

    foreach ($Website in $Websites) {

        $AppPool = Get-IISAppPool -Name $Website.Applications[0].ApplicationPoolName

        [PSCustomObject]@{
            WebsiteName                  = $Website.Name
            WebsiteId                    = $Website.Id
            WebsiteState                 = $Website.State
            WebsitePhysicalPath          = $Website.PhysicalPath
            WebsiteBindings              = $Website.Bindings.Collection -join ';'
            WebsiteAttributes            = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
            AppPoolName                  = $AppPool.Name
            AppPoolState                 = $AppPool.State
            AppPoolManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion
            AppPoolManagedPipelineMode   = $AppPool.ManagedPipelineMode
            AppPoolStartMode             = $AppPool.StartMode
        }
    }
} | Export-Excel -Path C:\users\$env:username\documents\IIS_sites.xlsx

推荐阅读