Solved

Changing powershell output from html to csv

Posted on 2014-02-03
6
972 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
  • 3
  • 3
6 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Do email signature updates give you a headache?

Do you feel like all of your time is spent managing email signatures? Too busy to visit every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 

Author Comment

by:hlaten
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
Qlemo did an outstanding job of providing to me what was requested.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now