【问题标题】:How to get the IIS site Physical path and the Binding details using power shell script如何使用 power shell 脚本获取 IIS 站点物理路径和绑定详细信息
【发布时间】:2020-01-29 14:26:26
【问题描述】:

我编写了一个 PS 脚本,其中会将所有 IIS 站点详细信息和应用程序池详细信息导出到 Excel 工作表,但是当我使用命令 Get-IISSite 时,物理路径和绑定详细信息未显示在输出控制台中,如下所示,代码如下,请帮助我解决导出 IIS 站点和应用程序池详细信息时遇到的问题

代码

#Clearing the Console host in PS
Clear-Host

$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]@{
            Website_Name                  = $Website.Name
            Website_Id                    = $Website.Id -join ';'
            Website_State                 = $Website.State -join ';'
            Website_PhysicalPath          = Get-ChildItem -Path IIS:\Sites\P #$Website.PhysicalPath -join ';'
            Website_Bindings              = $Website.Bindings.Collection -join ';'
            Website_Attributes            = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
            AppPool_Name                  = $AppPool.Name -join';'
            AppPool_State                 = $AppPool.State -join ';'
            AppPool_ManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion -join ';'
            AppPool_ManagedPipelineMode   = $AppPool.ManagedPipelineMode -join ';'
            AppPool_StartMode             = $AppPool.StartMode -join ';'
        }
    }
} | Export-Excel -Path C:\users\$env:username\documents\Site_App-pool_Details.xlsx -AutoSize -BoldTopRow

输出

Website_Name                  : Test
Website_Id                    : 2
Website_State                 : Started
Website_PhysicalPath          : 
Website_Bindings              : 
Website_Attributes            : name=Test;id=2;serverAutoStart=False;state=
                                1
AppPool_Name                  : Test
AppPool_State                 : Started
AppPool_ManagedRuntimeVersion : v4.0
AppPool_ManagedPipelineMode   : Integrated
AppPool_StartMode             : OnDemand
PSComputerName                : AAA
RunspaceId                    : 47d..

When i use the command **Get-Website** i will get all the output details of the IIS site but not the IIS App-pool details the code is a below

Output
Website_Name                  : Test
Website_Id                    : 2
Website_State                 : Started
Website_PhysicalPath          : C:\AAA
Website_Bindings              : http 10.62.:Test.com
Website_Attributes            : name=Test;id=2;serverAutoStart=False;state=
                                1
AppPool_Name                  : 
AppPool_State                 : 
AppPool_ManagedRuntimeVersion : 
AppPool_ManagedPipelineMode   : 
AppPool_StartMode             : 
PSComputerName                : AAA
RunspaceId                    : 15d..

请帮助我了解如何使用两者中的任何命令(Get-Website 或 Get-WebSite)来获取所有 IIS 站点和 App-pool 详细信息

提前致谢。

【问题讨论】:

  • 如果你使用Get-Website,那么apppool应该是Get-IISAppPool -Name $Website.ApplicationPool

标签: powershell


【解决方案1】:

你的代码中有一条奇怪的注释,它打破了它;但是,试试这个:

Get-Item IIS:\Sites\$Website | Select-Object -ExpandProperty physicalPath

而不是

Get-ChildItem -Path IIS:\Sites\$Website.PhysicalPath

对于绑定:

$Website.Bindings.bindingInformation

而不是

$Website.Bindings.Collection

【讨论】:

    【解决方案2】:

    您可以使用Get-WebsiteGet-IISAppPool 执行以下操作。

    $Websites = Get-Website
    
    foreach ($Website in $Websites) {
    
        $AppPool = Get-IISAppPool -Name $Website.ApplicationPool
    
        [PSCustomObject]@{
            Website_Name                  = $Website.Name
            Website_Id                    = $Website.Id -join ';'
            Website_State                 = $Website.State -join ';'
            Website_PhysicalPath          = $Website.PhysicalPath
            Website_Bindings              = $Website.Bindings.Collection -join ';'
            Website_Attributes            = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
            AppPool_Name                  = $AppPool.Name -join';'
            AppPool_State                 = $AppPool.State -join ';'
            AppPool_ManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion -join ';'
            AppPool_ManagedPipelineMode   = $AppPool.ManagedPipelineMode -join ';'
            AppPool_StartMode             = $AppPool.StartMode -join ';'
        }
    }
    

    样本输出

    Website_Name                  : Default Web Site
    Website_Id                    : 1
    Website_State                 : Started
    Website_PhysicalPath          : %SystemDrive%\inetpub\wwwroot
    Website_Bindings              : net.msmq localhost;msmq.formatname localhost;net.tcp 808:*;net.pipe *;http *:80:
    Website_Attributes            : name=Default Web Site;id=1;serverAutoStart=True;state=1
    AppPool_Name                  : DefaultAppPool
    AppPool_State                 : Started
    AppPool_ManagedRuntimeVersion : v4.0
    AppPool_ManagedPipelineMode   : Integrated
    AppPool_StartMode             : OnDemand
    

    所做的更改

    1. 使用Get-Website 填充$Websites
    2. $Website.ApplicationPool 传递到Get-IISAppPool-Name 参数中。
    3. Website_PhysicalPath 设置为$Website.PhysicalPath

    【讨论】:

    • 感谢您的帮助,我得到了我需要的确切输出。我需要更多帮助,你能告诉我如何在我的脚本中获取 excel 文件的完整标题的颜色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多