无法将 IIS 结果导出到 CSV

Unable to export the IIS result to CSV

我已经创建了一个脚本来从多个服务器获取所有 IIS 站点和应用程序池的详细信息,该脚本按预期工作正常,但我无法将结果导出到 CSV 文件。

如果有人建议我如何将输出详细信息导出到 CSV 文件,这将很有帮助。

代码

#Import-Module -Name WebAdministration

$Computers = Get-Content "C:\Desktop\Scripts\Servers.txt"

foreach ($server in $Computers) {

    $iis = Get-WmiObject Win32_Service -Filter "Name = 'IISADMIN'" -ComputerName $server

    if ($iis.State -eq 'Running') 
    { 
    $details = Write-Host "IIS is running on $server" -ForegroundColor Green 
    #Chandu
    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
        #$Website.ApplicationPool
        [PSCustomObject]@{
            Server_Name = $env:COMPUTERNAME +'.'+ $env:USERDNSDOMAIN
            Website_Name                  = $Website.Name
            Website_Id                    = $Website.Id -join ';'
            Website_State                 = $Website.State -join ';'
            Website_PhysicalPath          = $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-csv -Path "C:\Desktop\Scripts\IISite_App-pool_Details.csv" -Append
      }    
   }
} 
#Export-Excel -Append -Path C:\Desktop\Scripts\IISite_App-pool_Details.xlsx -AutoSize -BoldTopRow

您可以将开关 -HideComputerName 添加到 Invoke-Command 校准,但输出仍然会添加属性 'RunspaceId' 和 'PSShowComputerName'。

如果你不想要这些,你可以 de-select 从结果中得到它们

$iis = Get-WmiObject Win32_Service -Filter "Name = 'IISADMIN'" -ComputerName $server

if ($iis.State -eq 'Running') { 
    $details = Write-Host "IIS is running on $server" -ForegroundColor Green 
    #Chandu
    $result = 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
            #$Website.ApplicationPool
            [PSCustomObject]@{
                Server_Name                   = $env:COMPUTERNAME +'.'+ $env:USERDNSDOMAIN
                Website_Name                  = $Website.Name
                Website_Id                    = $Website.Id -join ';'
                Website_State                 = $Website.State -join ';'
                Website_PhysicalPath          = $Website.Applications["/"].VirtualDirectories["/"].PhysicalPath -join ';'
                Website_Bindings              = (Get-Website -Name $Website.Name).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 ';'
            }
        } 
    }
    # remove the extra properties Invoke-Command added
    $result = $result | Select-Object * -ExcludeProperty 'PSComputerName','RunspaceId','PSShowComputerName'
    # save the result as CSV file
    $result | Export-Csv -Path "C:\Desktop\Scripts\IISite_App-pool_Details.csv" -NoTypeInformation        
}

P.S。您的代码未显示您初始化变量 $Computers..

的位置