May 26, 2010

Checking Disk Usage

Here’s a function I have in my profile, I use it to do a quick disk usage check on servers:

Function Get-Disk {
	Param($computer)
	$disks = gwmi -computername $computer Win32_LogicalDisk -Filter 'DriveType = 3'
	$disks | %{
		New-Object PSObject -Property @{
		Drive=$_.DeviceID;
		Capacity="{0:n} GB" -f ($_.Size / 1GB);
		Used="{0:n} GB" -f ($_.FreeSpace / 1GB);		
		}
	}
}

Here’s an example of running this function:

[PS:8] Get-Disk server01

Drive Used      Capacity
----- ----      --------
C:    4.58 GB   68.25 GB
E:    64.18 GB  598.40 GB
F:    238.62 GB 598.40 GB
May 21, 2010

PowerShell Links for Friday 5/21/10

Get-Command: Not Just for Cmdlets

Here’s another miscellaneous tip..well, it’s not really that much of a tip but here goes…

You know you can use Get-Command to get information about cmdlets, but you can also get detailed info about executables, for example:

get-command cmd.exe | fl fileversioninfo,path

The above produces the following output:

[PS:28] get-command cmd.exe | fl fileversioninfo,path


FileVersionInfo : File:             C:\Windows\system32\cmd.exe
                  InternalName:     cmd
                  OriginalFilename: Cmd.Exe.MUI
                  FileVersion:      6.1.7600.16385 (win7_rtm.090713-1255)
                  FileDescription:  Windows Command Processor
                  Product:          Microsoft® Windows® Operating System
                  ProductVersion:   6.1.7600.16385
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:         English (United States)

Path            : C:\Windows\system32\cmd.exe

Quick List of PowerShell Profiles

I can never remember the names or locations of all the profiles, but I can remember this:

$profile | fl -Force

Here’s an example of the output:

[PS:27] $profile | fl -Force


AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Po...
CurrentUserAllHosts    : C:\Users\Administrator\Documents\WindowsPowerShell\prof...
CurrentUserCurrentHost : C:\Users\Administrator\Documents\WindowsPowerShell\Micr...
Length                 : 83

The output is truncated, but you get the idea…

Send an Email

PowerShell v2 has what seems to still be a relativley unknown cmdlet that allows you to send email. Here is an example of how to use it:

Send-MailMessage -To user@contoso.com `
				 -From user@fabrikam.com `
				 -Subject "Test" `
				 -Body "Test" `
				 -SmtpServer smtp.domain.com

On the otherhand, if you are still using v1, you can use the good ole System.Net.Mail classes:

function Send-Email {
	param($to, $from, $subject, $body, $smtpServer)
	
	$msg = new-object Net.Mail.MailMessage
	$smtp = new-object Net.Mail.SmtpClient($smtpServer)
	
	$msg.From = $from
	$msg.To.Add($to)
	$msg.Subject = $subject
	$msg.Body = $body
	
	$smtp.Send($msg)	
}

Validate an IP Address

Here is a function to validate an IP address. If valid, it returns $true, otherwise it returns $false.

function Test-IsValidIP ($ip) {
	if($ip -as [System.Net.IPAddress]) { $true } else { $false }
}