1

I am having a hard time creating a table that has multiple columns. The columns will be populated using various cmdlets. At the very end of a script, I want to run a final check and write the hostname to a file, along with various other columns. Here is what I was thinking for the headings: "HOSTNAME", "FILES REPLACED?", "SCCM UNINSTALLED?" The rows below these headings will show computer name, TRUE or FALSE, and TRUE or FALSE. So far, I am able to export the data to an HTM file, but it does not show up in separate columns. This is what I have:

$hostname | ConvertTo-Html -Property @{Expression={ $hostname};Label="Hostname"} | 
            Out-File C:\Temp\Test.htm -Append -NoClobber
If (Test-Path -Path $FileCheck) {
Test-path -Path $FileCheck | ConvertTo-Html -Property  @{Expression=`
    { [string]"TRUE"};Label="Windows files replace"} | Out-File C:\Temp\Test.htm `
                                                        -Append -NoClobber
}
Else {
Test-path -Path $FileCheck | ConvertTo-Html -Property @{Expression=`
    { [string]"FALSE"};Label="Windows files replaced"} | Out-File C:\Temp\Test.htm `
                                                         -Append -NoClobber
}

I basically just need to understand how to create a table or array with 3 or 4 columns and how to populate the rows for those columns. Each computer will have a different row and show something like (COMPUTER1,TRUE,FALSE,TRUE). The way I currently have it set up, it is creating new rows when I need the headings in new columns.

1 Answer 1

1

Ok, what you really need is an array of objects. Those objects will have three properties, one for each column in your table. To make an object in PS3 you can simply use the [pscustomobject] type followed by a hashtable containing the values that you want. Something like this:

[pscustomobject]@{
    "HOSTNAME" = $hostname
    "FILES REPLACED" = If (Test-Path -Path $FileCheck) {"TRUE"}else{"FALSE"}
    "SCCM UNINSTALLED" = <some test to see if SCCM is installed>
}

You could then create a loop for the computers/servers in question, create that object once for each machine, add all of the objects to an array, and then output that array to your HTML.

$Results = @()
ForEach($hostname in $machinelist){

    $filecheck = "\\$hostname\c$\path\to\files"
    $Results += [pscustomobject]@{
        "HOSTNAME" = $hostname
        "FILES REPLACED" = If (Test-Path -Path $FileCheck) {"TRUE"}else{"FALSE"}
        "SCCM UNINSTALLED" = <some test to see if SCCM is installed>
    }
}

$Results | ConvertTo-HTML -Property HOSTNAME,'FILES REPLACED','SCCM UNINSTALLED' | Set-Content C:\Temp\Test.htm

Obviously you will need to do some editing there, but that should give you something to build off of.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.