DCSIMG
March 2010 - Posts - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

March 2010 - Posts

The 2010 Scripting Games

sc2010This year the Scripting Games will take place April 26 through May 7. There are 20 scripting events (10 beginner and 10 advanced), in two scripting divisions: VBScript and PowerShell, and 40 guest commentators (including myself).

Like last year’s event, the scripts will be hosted on the PoshCode script repository. You will be able to log in using your Windows Live ID, or one of the OpenIDs that are available on the Internet.

If you use Twitter, you can search for tweets or mark your own with: #2010SG. It is the official games tag name. Don’t know where to start? Read the Frequently Asked Questions. The study guide is published HERE. Oh, did I mention prizes? :)

The Administrator's Guide to Windows PowerShell Remoting

‘Remoting is one of the most anticipated new features in Windows PowerShell 2.0. It turns Windows Powershell into an industrial strength management platform by enabling users to run Windows PowerShell commands on remote machines throughout the enterprise. However, setting up PowerShell remoting can be a daunting task. This guide helps users understand the concepts of remoting and provides essential and practical advice on setup, deployment and security.’

The ‘Administrator's Guide To Windows PowerShell Remoting‘ is a must read document, written by PowerShell MVPs Dr. Tobias Weltner (who is also the brilliant mind behind PowerShellPlus) and Aleksandar Nikolic, along with Richard Giles (Product Manager at Idera). The guide is available for free to all PowerShell.com members. So, if you're not a member already… now is time to join!

QuickTip: How do you check Internet connectivity?

With the following PowerShell one-liner you can check if the machine you’re working on has internet connectivity. The result is a Boolean value. If TRUE, the local machine is connected to the internet; if FALSE, it is not.

Minimum supported client: Windows Vista

[Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet

PowerShell to the rescue


disk A member of my team came in this morning with a scripting request:

"If we ran a full backup on all of our servers (fixed disks), how much disk space would they all consume altogether? We need to give the boss a ‘ball-park number’ by the end of the day!"

So, how can we get that number? Here’s a quick & dirty version.

1. Get all computers objects (names only). We keep our servers in a dedicated OU in active directory.
2. Run a WMI query on all servers and subtract each fixed disk FreeSpace from the total disk size.
3. Use Measure-Object to compute the sum of all of the values from the previous step.
4. Print the result in TB.

$servers = Get-QADComputer -SizeLimit 0 –SearchRoot ‘Domain.com/Servers’ | Select-Object -ExpandProperty Name
$disks = Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $servers -ErrorAction SilentlyContinue | Foreach-Object { $_.Size - $_.FreeSpace }
$totalDiskSpace = $Disks | Measure-Object -Sum
$totalDiskSpace.Sum/1TB

That wasn’t that hard. Needless to say, our boss didn’t have to wait all day long, he got the answer after only a few minutes!


EDIT: I've been asked about a way to get the server names without third party cmdlets.
The following shows how you can use the DirectorySearcher class. We initialize a new DirectorySearcher object and supply to arguments: Filter and PropertiesToLoad. The SearchRoot property tells the searcher where to start the search from (Servers OU).

$searcher = New-Object System.DirectoryServices.DirectorySearcher ‘(&(objectCategory=Computer)(objectClass=User))',’name’
$searcher.SearchRoot= "LDAP://OU=Servers,DC=domain,DC=com"
$servers = $searcher.FindAll() | Foreach-Object { $_.Properties["name"] }

New PowerShell Scripts feeds

The Scripting Guys added a new RSS feed for Windows PowerShell scripts at the TechNet Script Repository. The feed URL is: http://gallery.technet.microsoft.com/ScriptCenter/en-us/site/feeds/searchRss?f%5B0%5D.Type=ScriptLanguage&f%5B0%5D.Value=Powershell&f%5B0%5D.Text=Windows%20PowerShell&sortBy=Date

Want more? Thanks to jaykul, you can now get the Script Center and PoshCode PowerShell script repositories in one feed http://pipes.yahoo.com/powershell/scripts

iPowerShell V2 available at Apple app store

iPowerShell V1 was the first application I installed on my iPhone and V2 is now available at the Apple app store.  It is an easy to use reference tool for users of Microsoft's PowerShell scripting language for use on the iPhone or iPod Touch, It's FREE! If you already have iPowerShell V1 you should just be able to download the update. If you are not familiar with this wicked, cool product, check out SAPIEN’s blog post and read all about it.