Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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"
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.
share|improve this answer
    
Thanks...I selected yours as the answer because of the $profile information....however I wish I could select both since I did use Chris' "email portion" of his script he posted. –  TheCleaner Aug 6 '10 at 19:29
    
@TheCleaner Yeah, I misunderstood your email bit (was a bit not awake yet this morning). I was thinking you wanted someone to modify the script for your environment and send it to you in email.... (doh) –  squillman Aug 6 '10 at 19:40
    
BTW...I actually modified the final code and used the new PS2 command "Send-MailMessage" instead...I've updated my question with the final code for future searches –  TheCleaner Aug 6 '10 at 19:47
  1. If you copy and past it into a ps1 file, you can then run the file to load the function into your current PS environment.
  2. The function looks like it runs against WMI, which should work on everything WinXP and up.
  3. You can write a script that includes this function and the necessary info to send it in an e-mail, yes.

For example:

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

$emailFrom = "Automation@Example.com"
$emailTo = "Admin@Example.com"
$subject = "Free Disk Space Report"
$body = Get-Content .\servers.txt | Get-FreeDisk | Out-String
$smtpServer = "mail.example.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

Put that in a file named something like email_freedisk.ps1; then set a scheduled task to run it.

You just need a file with a list of servernames, one per line, named servers.txt.

share|improve this answer
    
good deal...see my comment to squillman as well –  TheCleaner Aug 6 '10 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.