?
Solved

Changing powershell output from html to csv

Posted on 2014-02-03
6
Medium Priority
?
1,226 Views
Last Modified: 2014-02-03
Can I be helped with changing the output of this script to either just csv or both csv and html output.

#requires -version 2.0
#use parameter  drive report to html.ps1 computer1,computer2 or a computer list file
#change file path and name on line 7 below to reflect name and  path of computer list file using.
#script will open web browser with current report when completed.
 
Param (
$computers = (Get-Content  "C:\Scripts\Computers.txt")
)
 
$Title="Hard Drive Report to HTML"
 
#embed a stylesheet in the html header
$head = @"
<mce:style><!--
mce:0
--></mce:style><style _mce_bogus="1"><!--
mce:0
--></style>
<Title>$Title</Title>
<br>
"@  
 
#define an array for html fragments
$fragments=@()
 
#get the drive data
$data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers
 
#group data by computername
$groups=$Data | Group-Object -Property SystemName
 
#this is the graph character
[string]$g=[char]9608  
 
#create html fragments for each computer
#iterate through each group object
         
ForEach ($computer in $groups) {
     
    $fragments+="<H2>$($computer.Name)</H2>"
     
    #define a collection of drives from the group object
    $Drives=$computer.group
     
    #create an html fragment
    $html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}},
    @{Name="SizeGB";Expression={$_.Size/1GB  -as [int]}},
    @{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }},
    @{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},
    @{Name="Usage";Expression={
      $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100
      $UsedGraph=$g * ($UsedPer/2)
      $FreeGraph=$g* ((100-$UsedPer)/2)
      #I'm using place holders for the < and > characters
      "xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph
    }} | ConvertTo-Html -Fragment  
     
    #replace the tag place holders. It is a hack but it works.
    $html=$html -replace "xopen","<"
    $html=$html -replace "xclose",">"
     
    #add to fragments
    $Fragments+=$html
     
    #insert a return between each computer
    $fragments+="<br>"
    #Adding log details..
    $log = Get-eventlog -list -ComputerName $computer.Name |
      where {$_.log -eq 'Security'} |
      Select Log,MaximumKilobytes,MinimumRetentionDays,@{Name="Entries";Expression={$_.Entries.count}},OverflowAction |
      ConvertTo-Html | Out-String
$fragments+= $log
} #foreach computer
 
#add a footer
$footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username)
$fragments+=$footer
 
#write the result to a file
ConvertTo-Html -head $head -body $fragments  | Out-File -append c:\Scripts\drivereport.htm
0
Comment
Question by:hlaten
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 39830025
Not feasible the same way. The function creates two sets of data not related to each other: Drive data and event log entries. You can't combine two different datasets into a single CSV, because a CSV has a fixed structure - each record has to be of the same structure.

Of course we can create two different CSV files.
0
 

Author Comment

by:hlaten
ID: 39830049
Will the two files be exported from the same script or is it two different scripts. The two files will do fine if that can be done. Thanks.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39830065
This creates two files (see last lines). The used/free graph is retained, slightly modified (as we don't have colors in CSV).
Param ( 
  $computers = (Get-Content  "C:\Scripts\Computers.txt") 
) 
 
#get the drive data 
$data = Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers 
 
#group data by computername 
$groups = $Data | Group-Object -Property SystemName 
 
#this is the graph character 
[string] $g = [char]9608  

$Drives = @()
$Logs   = @()

#iterate through each group object 
ForEach ($computer in $groups) { 
   
  #define a collection of drives from the group object 
  $Drives = $computer.group | Select 
    @{Name="Server"; Expression={$computer.Name},
    @{Name="Drive" ; Expression={$_.DeviceID}}, 
    @{Name="SizeGB"; Expression={$_.Size/1GB  -as [int]}}, 
    @{Name="UsedGB"; Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }}, 
    @{Name="FreeGB"; Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }}, 
    @{Name="Usage" ; Expression={ 
      $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100 
      $g * ($UsedPer/2) + '.' * ((100-$UsedPer)/2) 
    }}
   
  #Adding log details..
  $Logs += Get-eventlog -list -ComputerName $computer.Name | 
    where {$_.log -eq 'Security'} | 
    Select @{Name="Server"; Expression = {$computer.Name}}, Log,MaximumKilobytes,MinimumRetentionDays,@{Name="Entries";Expression={$_.Entries.count}},OverflowAction
} #foreach computer 

#write the result to a file 
$Drives | Export-CSV -NoType C:\Scripts\DriveReport.csv
$Logs   | Export-Csv -NoType C:\Scripts\Eventlogs.csv

Open in new window

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:hlaten
ID: 39830141
got this error first:

Missing '=' operator after key in hash literal.
At H:\scripts\drive_els_csv.ps1:33 char:11
+   $Logs += <<<<  Get-eventlog -list -ComputerName $computer.Name |
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral
I removed plus sign here:

$Logs += Get-eventlog -list -ComputerName $computer.Name |

Then got this error:

Missing closing '}' in statement block.
At H:\scripts\drive_els_csv1.ps1:40 char:54
+ $Logs   | Export-Csv -NoType H:\Scripts\Eventlogs.csv <<<<
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], Parse
   Exception
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39830277
No, the += was intentional and necessary. But there was a closing brace missing.
Param ( 
  $computers = (Get-Content  "C:\Scripts\Computers.txt") 
) 
 
#get the drive data 
$data = Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers 
 
#group data by computername 
$groups = $Data | Group-Object -Property SystemName 
 
[string] $g = '+'

$Drives = @()
$Logs   = @()

#iterate through each group object 
ForEach ($computer in $groups) { 
   
  #define a collection of drives from the group object 
  $Drives += $computer.group | Select `
    @{Name="Server"; Expression={$computer.Name}},
    @{Name="Drive" ; Expression={$_.DeviceID}}, 
    @{Name="SizeGB"; Expression={$_.Size/1GB  -as [int]}}, 
    @{Name="UsedGB"; Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }}, 
    @{Name="FreeGB"; Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }}, 
    @{Name="Usage" ; Expression={ 
      $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100 
      $g * ($UsedPer/2) + '.' * ((100-$UsedPer)/2) 
    }}
   
  #Adding log details..
  $Logs += Get-eventlog -list -ComputerName $computer.Name | 
    where {$_.log -eq 'Security'} | 
    Select @{Name="Server" ; Expression = {$computer.Name}},
           Log,MaximumKilobytes,MinimumRetentionDays,
           @{Name="Entries"; Expression = {$_.Entries.count}},
           OverflowAction
} #foreach computer 

#write the result to a file 
$Drives | Export-CSV -NoType C:\Scripts\DriveReport.csv
$Logs   | Export-Csv -NoType C:\Scripts\Eventlogs.csv

Open in new window

0
 

Author Closing Comment

by:hlaten
ID: 39830394
Qlemo did an outstanding job of providing to me what was requested.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question