locked
Need pwershell output in HTML RRS feed

  • Question

  • Hi All,

    The below mention code is for getting App Pool Status & require the HTML page of all server in ONE HTML. Output is generating only the HTML but not generating the content inside the HTML.


    $computers = Get-Content "D:\WebSite\ServerBaseAppstatus\Machines.txt"

    $ServerName = $computers.Split(",")

    $outfile = "D:\WebSite\ServerBaseAppstatus\services.html"

    foreach ($Machine in $ServerName) 
    {
           Write-Host "_________________________________"
           Write-Host "Details from server $Machine..."
           Write-Host "---------------------------------"
           $result = Invoke-command  -ComputerName $Machine  -ScriptBlock{ param($Machine)

                Import-Module WebAdministration
                set-Location IIS:\AppPools
                $appPoolCollections = dir
                foreach ($pool in $appPoolCollections)
                {
                    # Loop through the collection and find the status of the appPool 
                    $appPoolName = $pool.Name
                    $appPoolState = $pool.state
                    $appPoolVersion= $pool.managedRuntimeVersion
                    #Write-Host "$appPoolName with version $appPoolVersion is $appPoolState"  
                 }
            } 
            
            Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
            
            
      }
      


    Sumeet Gill


    • Edited by Sumeet Gill Tuesday, August 14, 2018 3:23 PM
    Tuesday, August 14, 2018 3:19 PM

Answers

  • This is how to do this in PowerShell:

    $Header = @'
    <style>
        table{
            border-width: 1px; 
            border-style: solid; 
            border-color: black; 
            border-collapse: collapse;
        }
        td{
            border-width: 1px; 
            padding: 3px; 
            border-style: solid; 
            border-color: black;
        }
    </style>
    '@
    
    $sb = {
        Try{ 
            Import-Module WebAdministration -ErrorAction Stop
            Get-ChildItem IIS:\AppPools -ErrorAction Stop
        }
        catch{
            Write-Host $_ -ForegroundColor Red
        }
    } 
    
    Get-Content D:\WebSite\ServerBaseAppstatus\Machines.txt |
        ForEach-Object{
           Invoke-command  -ComputerName $_ -ScriptBlock $sb
        } |
        Select-Object PsComputerName, Name, State, ManagedRuntimeVersion |
        ConvertTo-Html -Head $Header |
        Out-File $outfile

    Without "Stop" the Try/Catch is not functional.  There is no need for all of the messages or for custom objects. If you want to change the column headers use the select to rename the columns.

    Select-Object PsComputerName, @{n='PoolName';e={$_.Name}}, State, ManagedRuntimeVersion

    Always use the PowerShell built-in enumerator as it is more efficient and easier to modify as needs change.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 14, 2018 6:36 PM
    • Marked as answer by Sumeet Gill Thursday, August 16, 2018 7:09 AM
    Tuesday, August 14, 2018 6:35 PM

All replies

  • The error is that you need to give the argument for the scriptblock, so it's not bringing anything because the code within the scriptblock is not getting its argument. (to do so: see below)

    I've commented the Computers logic, it would have been easier to get the machines one by line and then you can use directly in the loop, no need to split and all of that.

    $computers = Get-Content "D:\WebSite\ServerBaseAppstatus\Machines.txt"
    #one computer by line
    
    foreach($comp in $computers){
     ....
    }

    #$computers = Get-Content "D:\WebSite\ServerBaseAppstatus\Machines.txt"
    #$ServerName = $computers.Split(",")
    $ServerName=@()
    $ServerName+="mail"
    #$outfile = "D:\WebSite\ServerBaseAppstatus\services.html"
    $outfile="$env:USERPROFILE\Desktop\here.html"
    
    $ToHtmlObject=@()
    $Header = @"
    <style>
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    </style>
    "@
    
    foreach ($Machine in $ServerName) 
    {
           Write-Host "_________________________________"
           Write-Host "Details from server $Machine..."
           Write-Host "---------------------------------"
           $result = Invoke-command  -ComputerName $Machine -ArgumentList $Machine  -ScriptBlock{ param($Machine)
            try{ 
                Import-Module WebAdministration
                Set-Location IIS:\AppPools
                $appPoolCollections = dir
                $Collections=@()
    
                foreach ($pool in $appPoolCollections){
                    # Loop through the collection and find the status of the appPool 
                    $Collections+= New-Object PSObject -Property @{"Computer"=$env:COMPUTERNAME;"appPoolName"=$pool.name;"appPoolState"=$pool.state;"appPoolVersion"=$pool.managedRuntimeVersion}
                }
                return $Collections
              }
              catch{
                Write-Host -ForegroundColor Red "There was an error: $($_exception.message)"
              }
            } 
            $result |ft
            Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
            $ToHtmlObject+=$result
            
      }
    
    
      $ToHtmlObject | ConvertTo-Html -Head $Header | Out-File $outfile

    The argument needs to be added with the parameter "argumentList"

    -argumentlist $Machinename
    if there were more than one argument, you just need to separate them with semicolons

    Also, you will just get the lines inside that output, so the output must be extracted or returned on the scriptblock to get something as "object" of it, you were just printing lines, and lines of text aren't convertible to HTML, objects do are convertibles








    • Proposed as answer by j0rt3g4 Tuesday, August 14, 2018 4:02 PM
    • Edited by j0rt3g4 Tuesday, August 14, 2018 4:10 PM
    • Unproposed as answer by j0rt3g4 Tuesday, August 14, 2018 6:11 PM
    Tuesday, August 14, 2018 4:02 PM
  • The truth is that you don't need the name of the computer in the parameters at all.

    #$computers = Get-Content "D:\WebSite\ServerBaseAppstatus\Machines.txt"
    #$ServerName = $computers.Split(",")
    $ServerName=@()
    $ServerName+="mail"
    #$outfile = "D:\WebSite\ServerBaseAppstatus\services.html"
    $outfile="$env:USERPROFILE\Desktop\here.html"
    
    $ToHtmlObject=@()
    $Header = @"
    <style>
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    </style>
    "@
    
    foreach ($Machine in $ServerName) 
    {
           Write-Host "_________________________________"
           Write-Host "Details from server $Machine..."
           Write-Host "---------------------------------"
           $result = Invoke-command  -ComputerName $Machine -ScriptBlock{
            try{ 
                Import-Module WebAdministration
                Set-Location IIS:\AppPools
                $appPoolCollections = dir
                $Collections=@()
    
                foreach ($pool in $appPoolCollections){
                    # Loop through the collection and find the status of the appPool 
                    $Collections+= New-Object PSObject -Property @{"Computer"=$env:COMPUTERNAME;"appPoolName"=$pool.name;"appPoolState"=$pool.state;"appPoolVersion"=$pool.managedRuntimeVersion}
                }
                return $Collections
              }
              catch{
                Write-Host -ForegroundColor Red "There was an error: $($_exception.message)"
              }
            } 
            $result |ft
            Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
            $ToHtmlObject+=$result
            
      }
    
    
      $ToHtmlObject | ConvertTo-Html -Head $Header | Out-File $outfile

    • Proposed as answer by j0rt3g4 Tuesday, August 14, 2018 6:12 PM
    Tuesday, August 14, 2018 6:12 PM
  • This is how to do this in PowerShell:

    $Header = @'
    <style>
        table{
            border-width: 1px; 
            border-style: solid; 
            border-color: black; 
            border-collapse: collapse;
        }
        td{
            border-width: 1px; 
            padding: 3px; 
            border-style: solid; 
            border-color: black;
        }
    </style>
    '@
    
    $sb = {
        Try{ 
            Import-Module WebAdministration -ErrorAction Stop
            Get-ChildItem IIS:\AppPools -ErrorAction Stop
        }
        catch{
            Write-Host $_ -ForegroundColor Red
        }
    } 
    
    Get-Content D:\WebSite\ServerBaseAppstatus\Machines.txt |
        ForEach-Object{
           Invoke-command  -ComputerName $_ -ScriptBlock $sb
        } |
        Select-Object PsComputerName, Name, State, ManagedRuntimeVersion |
        ConvertTo-Html -Head $Header |
        Out-File $outfile

    Without "Stop" the Try/Catch is not functional.  There is no need for all of the messages or for custom objects. If you want to change the column headers use the select to rename the columns.

    Select-Object PsComputerName, @{n='PoolName';e={$_.Name}}, State, ManagedRuntimeVersion

    Always use the PowerShell built-in enumerator as it is more efficient and easier to modify as needs change.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 14, 2018 6:36 PM
    • Marked as answer by Sumeet Gill Thursday, August 16, 2018 7:09 AM
    Tuesday, August 14, 2018 6:35 PM