Windows – PowerShell script to report free disk space on servers

disk-space-utilizationpowershellscriptingwindows

I am required to get reports about free disk space on our exchange servers.
I found this link : https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-Sample-f7164554

But I am unable to successfully run it…anybody got a better/easier way to do this?
I don't need the report as email…just as a csv file.
I'm using windows 10..and $PSVersionTable shows:

Name                           Value    
PSVersion                      5.1.14393.3053                                                                                                                                                 
PSEdition                      Desktop                                                                                                                                                        
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                        
BuildVersion                   10.0.14393.3053                                                                                                                                                
CLRVersion                     4.0.30319.42000                                                                                                                                                
WSManStackVersion              3.0                                                                                                                                                            
PSRemotingProtocolVersion      2.3                                                                                                                                                            
SerializationVersion           1.1.0.1

The Error I get:

-ErrorAction : The term '-ErrorAction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\scripts\getdiskspace.ps1:7 char:1
+ -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ErrorAction:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Same for -ComputerName however the weird thing is that it still runs and creates the csv file but instead of 4 servers it has the disk size listings of only the localhost

I was able to make this script send the email and I can use task cheduler to run it weekly, Just need to figure out this error and Why its displaying only one server(locolhost) disk size. Here's the exact script I used:

$File = Import-csv C:\scripts\getdiskspace.csv
$DiskReport = ForEach ($Servernames in ($File))  
 
{
Get-WmiObject win32_logicaldisk <#-Credential $RunAccount#> ` 
-ComputerName $Servernames -Filter "Drivetype=3" ` 
-ErrorAction SilentlyContinue 
 
#return only disks with 
#free space less   
#than or equal to 0.1 (10%) 
 
#Where-Object {   ($_.freespace/$_.size) -le '0.1'} 
 
}  
 
 
#create reports 
 
$DiskReport |  
 
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}}, 
@{Label = "Drive Letter";Expression = {$_.DeviceID}}, 
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}}, 
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }}, 
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} | 
 
#Export report to CSV file (Disk Report) 
 
Export-Csv -path "C:\scripts\DiskReport_$logDate.csv" -NoTypeInformation 
  
#Send disk report using the exchange email module 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; 
 
#Import-Module "\\Servername\ServerStorageReport\ExchangeModule\Exchange.ps1" -ErrorAction SilentlyContinue 
 
#Attach and send CSV report (Most recent report will be attached) 
 
$messageParameters = @{                         
                Subject = "Weekly Server Storage Report"                         
                Body = "Attached is Weekly Server Storage Report.All reports are located in C:\scripts\, but the most recent  is sent weekly"                    
                From = "Email name1 <admin@example.com>"                         
                To = "Email name1 <admin@example.com>" 
                CC = "Email name2 <user@example.com>" 
                Attachments = (Get-ChildItem C:\scripts\*.* | sort LastWriteTime | select -last 1)                    
                SmtpServer = "mail.example.com"                         
            }     
Send-MailMessage @messageParameters -BodyAsHtml

—–Below is what my csv looks like:

Servernames
server1
server2
server3
server4

Best Answer

ok this worked :

$LogDate = get-date -f yyyyMMddhhmm
$File = Get-Content -Path C:\StorageReport\Servers.txt

$DiskReport = ForEach ($Servernames in ($File)) 

{Get-WmiObject win32_logicaldisk <#-Credential $RunAccount#> `
-ComputerName $Servernames -Filter "Drivetype=3" `
-ErrorAction SilentlyContinue 
} 

$DiskReport | 

Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |

Export-Csv -path "C:\StorageReport\DiskReport_$logDate.csv" -NoTypeInformation

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; 

$messageParameters = @{                        
                Subject = "Weekly Server Storage Report"                        
                Body = "Attached is Weekly Server Storage Report.All reports are located in C:\StorageReport\, but the                

          most recent  is sent weekly"                   
                From = "Email name1 <Email.name1@domainname.com>"                        
                To = "Email name1 <Email.name1@domainname.com>"
                CC = "Email name2 <Email.name2@domainname.com>"
                Attachments = (Get-ChildItem C:\StorageReport\*.* | sort LastWriteTime | select -last 1)                   
                SmtpServer = "SMTPServerName.com"                        
            }   
Send-MailMessage @messageParameters -BodyAsHtml