EDIT: QUESTION HAS BEEN ANSWERED SEE THE BOTTOM OF MY POST FOR MY FINAL SCRIPT…
Man, I'm a powershell noob…
I have this link:
http://gallery.technet.microsoft.com/ScriptCenter/en-us/da3fee00-e79d-482b-91f2-7c729c38068f
I'd like to use that to take a list of servers, run it against that list, and then get a report of each server with disk space similar to:
SERVER1
C: Total=120GB Free Space=60GB
D: Total=400GB Free Space=200GB
etc.
The problem is…
-
I don't know what to do with the script on that link to get it to work. Copy/paste it into notepad and save as a .ps1?? Doesn't seem to be that way in the description.
-
Will it work if I run it from my Win7 box with PS, or does every server have to have Powershell installed for a remote PS script to work?
-
Is there a way to setup the script to email me? That way I can set it as a weekly task or similar.
Thank you!
========
Final script
function Get-FreeDisk
{
[CmdletBinding()]
param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[string[]]$Computername="localhost",
[int]
[ValidateRange(0,100)]
$MinPercFree = 100,
[Management.Automation.PSCredential]$Credential = ([Management.Automation.PSCredential]::Empty)
)
begin{
[String[]]$computers = @()
}
process{
$computers += $Computername
}
end{
Get-WmiObject -computername $computers -Credential $Credential `
-Query "select __SERVER, Caption,Label,Capacity,FreeSpace from Win32_Volume where DriveType != 5 and Capacity > 0" | `
Add-Member -Name Free -MemberType ScriptProperty -PassThru -Value {($this.FreeSpace/10000) / ($this.Capacity/1000000)} | `
Where { $_.Free -lt $MinPercFree } | `
sort __SERVER,Caption | `
Format-Table @{Label="Computer"; Expression={$_.__SERVER}}, Caption, Label,`
@{Label="Size/MB"; FormatString="{0,7:N0}"; Expression={$_.Capacity / 1mb}},`
@{Label="FreeSpace/MB"; FormatString="{0,7:N0}"; Expression={$_.Freespace / 1mb}}, `
@{Label="Free"; FormatString="{0,3:N0}%"; Expression={$_.Free}} -AutoSize
}
}
Get-Content .\servers.txt | Get-FreeDisk | Format-Table -AutoSize | Out-File diskusage.txt
Send-MailMessage -To thecleaner@domain.com -Subject "Server Disk Report" -From DiskReport@domain.com -SmtpServer mail.domain.com -Attachments "diskusage.txt"
Best Answer
To your points:
It's a function so you'll need to load it into your environment. You can do that a couple of ways.
To add it to your profile:
notepad $profile
, this will launch notepad with your profile script in it. The profile script is something that gets executed every time you start a PS session and lets you do things automatically like set environment variables, aliases, session functions (such as this one) etc. Note that this file doesn't exist by default so you may have to create it if you've not done so already. The path is%USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
(use My Documents instead of Documents for XP)function
and ending with the final}
. You can include the line that saysnew-alias
as well if you want but it's not necessary. Thatnew-alias
command will make it so that you can typedf
instead of having to type the wholeGet-Freedisk
command. $profile
to make the changes take effect.