Powershell help: have a “disk space” script link…but how to use it

powershell

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…

  1. 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.

  2. 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?

  3. 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:

  1. See below
  2. Remote machines don't need to have PS installed on them. Only the machine you're running the command from needs to have PS
  3. It should work as-is by following the directions below. You shouldn't need to customize it for your environment.

It's a function so you'll need to load it into your environment. You can do that a couple of ways.

  1. by typing it in in your current session
  2. add it to your profile (my preferred method in this case)

To add it to your profile:

  1. in PS, type 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)
  2. paste that code into the notepad window, starting with function and ending with the final }. You can include the line that says new-alias as well if you want but it's not necessary. That new-alias command will make it so that you can type df instead of having to type the whole Get-Freedisk command
  3. save and close the file
  4. restart PS, or type . $profile to make the changes take effect.
Related Topic