Append output to HTML file using ConvetTo-html

I am reading computer names from a cvs file and getting system information and exporting the information to a HTML file. I am testing two computers (computer1 and computer2); however, I only get the output for computer2. How can I append the output to an HTML?. Here is the code.

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Import-Csv $cvsFile 



Clear-Host

$filepath = (Get-ChildItem env:\userprofile).value 
 
 
Add-Content  "$Filepath\style.CSS"  -Value " body { 
font-family:Calibri; 
 font-size:10pt; 

th {  
background-color:black; 
 
color:white; 

td { 
 background-color:#19fff0; 
color:black; 
}" 
 
Write-Host "CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!" -ForegroundColor Yellow 


Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter "IpEnabled = True"| select -ExpandProperty macaddress


$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion



ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri  "$filepath\style.CSS" -Title "Server Inventory" | Out-File "$FilePath\$ComputerName.html" 



}


Write-Host "Script Execution Completed" -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\$ComputerName.html"

May 28th, 2015 4:00pm

Hi,

there are two little errors in it.

1)

$a = Import-Csv $cvsFile

This reads the entire file and the foreach loop can't read it line by line. Try this:

$a = Get-Content "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

2)

you have your Out-File in the foreach loop like this:

foreach(){

$computername = $comp.Name "$filepath\style.CSS" | Out-File "$FilePath\$ComputerName.html"

}

So every loop iteration the variable changes to the computer name.

If you want only one html file with all the servers in it, it should look like this

(no variable, just a file name with "-Append" after that):

"$filepath\style.CSS" | Out-File "$FilePath\ServerInventory.html" -Append

Invoke-Item -Path "$FilePath\ServerInventory.html"

Here is the working code:

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Get-Content $cvsFile

$filepath = 'C:\Temp' 

Add-Content  "$Filepath\style.CSS"  -Value ' body { 
font-family:Calibri; 
 font-size:10pt; 
} 
th {  
background-color:black; 
 
color:white; 
} 
td { 
 background-color:#19fff0; 
color:black; 
}' 
 
Write-Host 'CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!' -ForegroundColor Yellow 

Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter 'IpEnabled = True'| Select-Object -ExpandProperty macaddress

$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ', Service Pack: ' + $computerOS.ServicePackMajorVersion

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri"

$filepath\style.CSS" -Title 'Server Inventory' | Out-File "$FilePath\Report.html" -Append

}

Write-Host 'Script Execution Completed' -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\Report.html"


Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 6:17pm

Hi,

there are two little errors in it.

1)

$a = Import-Csv $cvsFile

This reads the entire file and the foreach loop can't read it line by line. Try this:

$a = Get-Content "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

2)

you have your Out-File in the foreach loop like this:

foreach(){

$computername = $comp.Name "$filepath\style.CSS" | Out-File "$FilePath\$ComputerName.html"

}

So every loop iteration the variable changes to the computer name.

If you want only one html file with all the servers in it, it should look like this

(no variable, just a file name with "-Append" after that):

"$filepath\style.CSS" | Out-File "$FilePath\ServerInventory.html" -Append

Invoke-Item -Path "$FilePath\ServerInventory.html"

Here is the working code:

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Get-Content $cvsFile

$filepath = 'C:\Temp' 

Add-Content  "$Filepath\style.CSS"  -Value ' body { 
font-family:Calibri; 
 font-size:10pt; 
} 
th {  
background-color:black; 
 
color:white; 
} 
td { 
 background-color:#19fff0; 
color:black; 
}' 
 
Write-Host 'CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!' -ForegroundColor Yellow 

Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter 'IpEnabled = True'| Select-Object -ExpandProperty macaddress

$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ', Service Pack: ' + $computerOS.ServicePackMajorVersion

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri"

$filepath\style.CSS" -Title 'Server Inventory' | Out-File "$FilePath\Report.html" -Append

}

Write-Host 'Script Execution Completed' -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\Report.html"


May 28th, 2015 6:21pm

Hi,

there are two little errors in it.

1)

$a = Import-Csv $cvsFile

This reads the entire file and the foreach loop can't read it line by line. Try this:

$a = Get-Content "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

2)

you have your Out-File in the foreach loop like this:

foreach(){

$computername = $comp.Name "$filepath\style.CSS" | Out-File "$FilePath\$ComputerName.html"

}

So every loop iteration the variable changes to the computer name.

If you want only one html file with all the servers in it, it should look like this

(no variable, just a file name with "-Append" after that):

"$filepath\style.CSS" | Out-File "$FilePath\ServerInventory.html" -Append

Invoke-Item -Path "$FilePath\ServerInventory.html"

Here is the working code:

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Get-Content $cvsFile

$filepath = 'C:\Temp' 

Add-Content  "$Filepath\style.CSS"  -Value ' body { 
font-family:Calibri; 
 font-size:10pt; 
} 
th {  
background-color:black; 
 
color:white; 
} 
td { 
 background-color:#19fff0; 
color:black; 
}' 
 
Write-Host 'CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!' -ForegroundColor Yellow 

Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter 'IpEnabled = True'| Select-Object -ExpandProperty macaddress

$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ', Service Pack: ' + $computerOS.ServicePackMajorVersion

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri"

$filepath\style.CSS" -Title 'Server Inventory' | Out-File "$FilePath\Report.html" -Append

}

Write-Host 'Script Execution Completed' -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\Report.html"


Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 10:16pm

Hi,

there are two little errors in it.

1)

$a = Import-Csv $cvsFile

This reads the entire file and the foreach loop can't read it line by line. Try this:

$a = Get-Content "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

2)

you have your Out-File in the foreach loop like this:

foreach(){

$computername = $comp.Name "$filepath\style.CSS" | Out-File "$FilePath\$ComputerName.html"

}

So every loop iteration the variable changes to the computer name.

If you want only one html file with all the servers in it, it should look like this

(no variable, just a file name with "-Append" after that):

"$filepath\style.CSS" | Out-File "$FilePath\ServerInventory.html" -Append

Invoke-Item -Path "$FilePath\ServerInventory.html"

Here is the working code:

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Get-Content $cvsFile

$filepath = 'C:\Temp' 

Add-Content  "$Filepath\style.CSS"  -Value ' body { 
font-family:Calibri; 
 font-size:10pt; 
} 
th {  
background-color:black; 
 
color:white; 
} 
td { 
 background-color:#19fff0; 
color:black; 
}' 
 
Write-Host 'CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!' -ForegroundColor Yellow 

Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter 'IpEnabled = True'| Select-Object -ExpandProperty macaddress

$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ', Service Pack: ' + $computerOS.ServicePackMajorVersion

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri"

$filepath\style.CSS" -Title 'Server Inventory' | Out-File "$FilePath\Report.html" -Append

}

Write-Host 'Script Execution Completed' -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\Report.html"


May 28th, 2015 10:16pm

Hi,

there are two little errors in it.

1)

$a = Import-Csv $cvsFile

This reads the entire file and the foreach loop can't read it line by line. Try this:

$a = Get-Content "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

2)

you have your Out-File in the foreach loop like this:

foreach(){

$computername = $comp.Name "$filepath\style.CSS" | Out-File "$FilePath\$ComputerName.html"

}

So every loop iteration the variable changes to the computer name.

If you want only one html file with all the servers in it, it should look like this

(no variable, just a file name with "-Append" after that):

"$filepath\style.CSS" | Out-File "$FilePath\ServerInventory.html" -Append

Invoke-Item -Path "$FilePath\ServerInventory.html"

Here is the working code:

$cvsFile = "C:\scripts\Get Macaddress remote computers\GetMacaddressERN\ComputerName .csv"

$a = Get-Content $cvsFile

$filepath = 'C:\Temp' 

Add-Content  "$Filepath\style.CSS"  -Value ' body { 
font-family:Calibri; 
 font-size:10pt; 
} 
th {  
background-color:black; 
 
color:white; 
} 
td { 
 background-color:#19fff0; 
color:black; 
}' 
 
Write-Host 'CSS File Created Successfully... Executing Inventory Report!!! Please Wait !!!' -ForegroundColor Yellow 

Foreach ( $computerSystem in $a ) {

$comp = Get-CimInstance CIM_ComputerSystem -ComputerName $computerSystem.Pcname
$computerBIOS = Get-CimInstance CIM_BIOSElement -ComputerName $computerSystem.Pcname
$computerOS = Get-CimInstance CIM_OperatingSystem -ComputerName $computerSystem.Pcname
$computerCPU = Get-CimInstance CIM_Processor -ComputerName $computerSystem.Pcname
$computerHDD = Get-CimInstance Win32_LogicalDisk -ComputerName $computerSystem.Pcname  -Filter "DeviceID = 'C:'"
$macaddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $computerSystem.Pcname  -filter 'IpEnabled = True'| Select-Object -ExpandProperty macaddress

$manufacturer = $comp.Manufacturer
$computername = $comp.Name 
$model = $comp.Model
$serialnumber = $computerBIOS.SerialNumber
$CPU = $computerCPU.Name
$OS = $computerOS.caption + ', Service Pack: ' + $computerOS.ServicePackMajorVersion

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Computer Name: </B></H4></font> $computername
<font color = blue><H4><B>Manufacturer: </B></H4></font> $manufacturer
<font color = blue><H4><B>Model: </B></H4></font>$model 
<font color = blue><H4><B>Serial Number: </B></H4></font>$serialnumber
<font color = blue><H4><B>CPU: </B></H4></font>$CPU
<font color = blue><H4><B>Operating System: </B></H4></font>$OS
<font color = blue><H4><B>Mac Address</B></H4></font>$macaddress" -CssUri"

$filepath\style.CSS" -Title 'Server Inventory' | Out-File "$FilePath\Report.html" -Append

}

Write-Host 'Script Execution Completed' -ForegroundColor Yellow 
Invoke-Item -Path "$FilePath\Report.html"


Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 10:16pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics