Append output to HTML file using ConvetTo-html
i reading computer names cvs file , getting system information , exporting information html file. testing 2 computers (computer1 , computer2); however, output computer2. how can append output html?. here 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"
hi,
there 2 little errors in it.
1)
$a = import-csv $cvsfile
this reads entire file , foreach loop can't read line line. try this:
$a = get-content "c:\scripts\get macaddress remote computers\getmacaddressern\computername .csv"
2)
you have out-file in foreach loop this:
foreach(){
$computername = $comp.name "$filepath\style.css" | out-file "$filepath\$computername.html"
}
so every loop iteration variable changes computer name.
if want 1 html file servers in it, should
(no variable, file name "-append" after that):
"$filepath\style.css" | out-file "$filepath\serverinventory.html" -append invoke-item -path "$filepath\serverinventory.html"
here 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"
Windows Server > Windows PowerShell
Comments
Post a Comment