Solved

Challenge of combining two powershell scripts

Posted on 2014-01-29
2
434 Views
Last Modified: 2014-01-30
Challenge for the experts, not a critical request:

Is it possible to combine two powershell scripts together that are listed below. I need to combine the eventlogsize script into GetDrivesize and have the output in html as in the GetDriveSize script. What I am trying to do is pull down the hard drive size for each hard drive on each computer and also pull down the security event log maximum size, and then package the output into and html file for veiwing and sharing.

GetDriveSize script:

#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>"
     
} #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


GetEventLogsize:

get-eventlog -list -ComputerName DUNtdc00,duntdc01 | where {$_.log -eq 'Security'} | Select Machinename,Log,MaximumKilobytes,MinimumRetentionDays,@{Name="Entries";Expression={$_.Entries.count}},OverflowAction
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
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39820566
Simple option is to convert the output of GetEventLogsize code and merge it with the result of GetDriveSize script.. Check this and see if it works for you..
#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

Open in new window

0
 

Author Closing Comment

by:hlaten
ID: 39820707
Thanks for the quick repsonse anscript does an excellent job as 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

How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

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