Home > Enterprise >  Unable to export the IIS result to CSV
Unable to export the IIS result to CSV

Time:02-26

I have created a script to get all the IIS Sites and App-pool details from multiple servers, the script is working fine as expected, but I am unable to export the results to CSV file.

It will helpful if someone suggest on how can I export the output details to CSV file.

CODE

#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

CodePudding user response:

You could add switch -HideComputerName to the Invoke-Command cal, but then still the output will also have properties 'RunspaceId' and 'PSShowComputerName' added to it.

If you don't want those, you can de-select them from the result like

$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. Your code doesn't show where you initialized variable $Computers..

  • Related