Solved

Changing powershell output from html to csv

Posted on 2014-02-03
6
1,122 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 69

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 69

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 69

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
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 …

730 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