Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing powershell output from html to csv

Posted on 2014-02-03
6
Medium Priority
?
1,319 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 71

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 71

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 71

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

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.

Question has a verified solution.

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

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

597 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