Link to home
Start Free TrialLog in
Avatar of SubSun
SubSunFlag for India

asked on

Need to modify Powershell script

Hello Experts,

I got the attached script from one of the EE post (Thanks to Chris-Dent). I would like to modify the same script to get an output as shown in screen shot by email.

What need to be added?

1, Need to receive the report via email.
2, Percentage used should change to percentage free.
3, The disks which have less than 20% free space should be highlighted.
4, Free space details of mounted volume should be listed in report.
Get-WmiObject Win32_LogicalDisk -Filter "DriveType='3'" `
  -ComputerName (Get-Content "c:\Systems Administration\Scripts\Servers.txt") | `
  Format-Table `
    @{l="Server";e={$_.SystemName}}, `
    @{l="Drive Letter";e={$_.DeviceID}}, `
    @{l="Free Space on Disk (GB)";e={"{0:n2}" -f ($_.freespace/1gb)}}, `
    @{l="Total Disk Space (GB)";e={"{0:n2}" -f ($_.size/1gb)}}, `
    @{l="Percentage Used";e={ "{0:P2}" -f (1 - ([Int64]$_.FreeSpace / [Int64]$_.Size)) }}

Open in new window

report.JPG
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SubSun

ASKER

Excellent.!! It works 95%, need to correct few things.
1, Its not displaying details of mount points.
2, Some of the volumes which is more than 20% free space also high lighted.
3, Can I have the output inside table, as I shown in the screen shot?
Avatar of SubSun

ASKER

See the report (I have removed the server name portion)
report.JPG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SubSun

ASKER

Does the Win32_Volume Class gives the information about mount points?
Avatar of SubSun

ASKER

Also Can you explain the following part? suppose if I want to change the 20% to 10% then how do I modify the calculation?

# Convert to HTML and highlight rows less than 20%
$HtmlBody = $VolumeInformation | ConvertTo-Html -Head $Style | %{
If ($_ -Match ".*(\d|1\d)(\.\d*)?\s%.*") {
$_ -Replace "", "" 
} Else { $_ }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SubSun

ASKER

Thanks a lot for explaining in detail.

I have done some R&D on Win32_volume and found the following code to get the details, but it also takes the DVD drive and throws the error while calculating %, how do we put filter for "DriveType='3'??

Select-Object : Attempted to divide by zero.
At line:2 char:14
+ Select-Object  <<<< `
==============================
Command
==============================
Get-WmiObject Win32_volume -ComputerName Server01 | where {$_.caption -like "*:\*"} | `
Select-Object `
@{n="Server";e={$_.__SERVER}}, `
@{n="Volume";e={$_.caption}}, `
@{n="Free Space on Volume (GB)";e={"{0:n2}" -f ($_.freespace/1gb)}}, `
@{n="Total Space on Volume (GB)";e={"{0:n2}" -f ($_.capacity/1gb)}}, `
@{n="Percentage Free";e={ "{0:P2}" -f ([Int64]$_.FreeSpace / [Int64]$_.capacity) }}
 
==========================
Result
==========================
 
Server                     : Server01
Volume                     : C:\
Free Space on Volume (GB)  : 2.90
Total Space on Volume (GB) : 15.99
Percentage Free            : 18.14 %
 
Server                     : Server01
Volume                     : M:\
Free Space on Volume (GB)  : 48.03
Total Space on Volume (GB) : 49.99
Percentage Free            : 96.08 %
 
Server                     : Server01
Volume                     : C:\mount\Server01\log\
Free Space on Volume (GB)  : 65.70
Total Space on Volume (GB) : 67.09
Percentage Free            : 97.93 %
 
Server                     : Server01
Volume                     : C:\mount\Server01\mbox_234\
Free Space on Volume (GB)  : 18.70
Total Space on Volume (GB) : 200.00
Percentage Free            : 9.35 %
 
Server                     : Server01
Volume                     : C:\mount\Server01\mbox_123\
Free Space on Volume (GB)  : 22.08
Total Space on Volume (GB) : 253.48
Percentage Free            : 8.71 %
 
Select-Object : Attempted to divide by zero.
At line:2 char:14
+ Select-Object  <<<< `
Server                     : Server01
Volume                     : E:\
Free Space on Volume (GB)  : 0.00
Total Space on Volume (GB) : 0.00

Open in new window

Avatar of SubSun

ASKER

OK got it.. :-)
Get-WmiObject Win32_volume -ComputerName server01 | where {(($_.DriveType -like "3") -and ($_.caption -like "*:\*"))} | `
Select-Object `
@{n="Server";e={$_.__SERVER}}, `
@{n="Volume";e={$_.caption}}, `
@{n="Free Space on Volume (GB)";e={"{0:n2}" -f ($_.freespace/1gb)}}, `
@{n="Total Space on Volume (GB)";e={"{0:n2}" -f ($_.capacity/1gb)}}, `
@{n="Percentage Free";e={ "{0:P2}" -f ([Int64]$_.FreeSpace / [Int64]$_.capacity) }}

Open in new window

Avatar of SubSun

ASKER

Chris, in HTML result, how do we make the font look small?
Avatar of SubSun

ASKER

OK, I got it-- Added the font-size and now my report looks perfect.. :-)
Avatar of SubSun

ASKER

Can I align only volume column data to left?.. :-)

Should be able to, that's what the "<col>" tags are for. Try editing the HTML (perhaps in notepad) to see if it'll do what you want?

Chris
Avatar of SubSun

ASKER

When I try to align to left all the columns goes left, not able to align the single column,, :(

Hmm how did you add it?

I had something like this in mind.

In the Style:

col.alignleft { text-align: left; }

In the table:

<colgroup>
  <col>
  <col class="alignleft">
  <col>
  <col>
  <col>
</colgroup>

Although I haven't actually tried it so I can't be sure it works...

Chris
Avatar of SubSun

ASKER

:( same issue.. this is not mandatory. but if we can do it, well & good, you can research on it when you get time..:-) I will also do some research on my end..
Avatar of collaberait
collaberait

Hi Chris,

I ran the same script & it throws me the error MS VB Script compilation error.I am a novice in scripting & need your help to get the disk space percentage for all servers using a input file.Please help.