Solved

Changing powershell output from html to csv

Posted on 2014-02-03
6
1,168 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

726 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