简体   繁体   中英

Array Convert to html in powershell

Hi everyone, I have a powershell script to get macfee anti-virus version from a list of servers. Here is the script:

$a = @()
$serverlist = "serverlist.txt"
foreach ($server in Get-Content $serverlist) 
{ 
$ProductVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\DesktopProtection').GetValue('szProductVer')
$EngineVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('EngineVersionMajor')
$DatVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('AVDatVersion')
$DatDate = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('AVDatDate')

$a += " `n $server - Product version: $ProductVer Engine version: $EngineVer Dat version: $DatVer DatDate $DatDate "
# Write-Host "$server - Product version: $ProductVer Engine version: $EngineVer Dat version: $DatVer DatDate $DatDate"

}

 Write-host $a

 # foreach($_ in $a){ConvertTo-HTML -Body $_,$a.$_ | Out-File D:\Powershell_scripts\list.html}

The write host output gives me this output on screen:

aucfd208 - Product version: 8.8.0.975 Engine version: 5600 Dat version: 7389 DatDate 2014/03/26
aucfd207 - Product version: 8.8.0.975 Engine version: 5600 Dat version: 7389 DatDate 2014/03/26
au420g18 - Product version: 8.8.0.975 Engine version: 5600 Dat version: 7389 DatDate 2014/03/26
cnche001 - Product version: 8.8.0.975 Engine version: 5600 Dat version: 7389 DatDate 2014/03/26

Now I'm confused on how to get in an html format?

If I use foreach($_ in $a){ConvertTo-HTML -Body $_,$a.$_ | Out-File D:\\Powershell_scripts\\list.html foreach($_ in $a){ConvertTo-HTML -Body $_,$a.$_ | Out-File D:\\Powershell_scripts\\list.html , I only get the last line of output:

cnche001 - Product version: 8.8.0.975 Engine version: 5600 Dat version: 7389 DatDate 2014/03/26

Any help would be appreciated.

Cheers

Depends how you want the output. If you just want the $a array in the body you can do:

ConvertTo-HTML -Body $a | Out-File D:\\Powershell_scripts\\list.html

I would try slightly different approach - first take the input into csv-like form. This should be fairly easy:

$a = @()
$serverlist = "serverlist.txt"
$outputCSV = "report.csv"
"Server;ProductVer;EngineVer;DatVer;DatDate" | Out-File -Append $outputCSV
foreach ($server in Get-Content $serverlist) 
{ 
$ProductVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\DesktopProtection').GetValue('szProductVer')
$EngineVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('EngineVersionMajor')
$DatVer = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('AVDatVersion')
$DatDate = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer).OpenSubKey('SOFTWARE\McAfee\AVEngine').GetValue('AVDatDate')

$Server + ";" + $ProductVer + ";" + $EngineVer + ";" + $DatVer + ";" + $DatDate | out-file -Append $outputCSV
} 

$CSVform = Import-Csv -Delimiter ";" $outputCSV
$CSVform | ConvertTo-Html | Out-File "HTMLReport.html"

This should generate a simple HTML output you are looking for. There will be no eye candy and stuff, but this is up to your invention how you want the script to work :-)

As for the question why your script gave only last line, I assume it is because it was missing -Append switch - each line was overwritten with previous one. Similar to '>' and '>>' redirector in command-line.

Hope that clarifies a bit.

Best regards AlexP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM