Tuesday, July 6, 2010

Have Some Fun With Powershell

Wow, two posts in one day. That's a record.

So I was on Twitter and saw @alasta tweet about Get-Process | Stop-Process, of which I replied with Get-Process | Get-Random | Stop-Process, basically Process roulette as we call it.

Was then thinking too bad that Stop-Process doesn't support remote computers, however have no fear, the built in taskkill of Windows will do the trick.

First off, I do not recommend using this, second thing to say it is kind of funny. Try this next time you want to annoy someone:

Get-Process -ComputerName $colleaguepc | Get-Random | ForEach-Object {Taskkill /f /s \\$colleaguepc /pid $_.pid }

Again, use at your own risk!

Where the heck did the time go?

Wow, what the heck happened?

As the great Scottish poet Robbie Burns wrote, "The best laid schemes o' mice an' men", I started this blog with good intentions. To let those in my sphere of influence know the cool things about Powershell. Then of course life takes another turn and I had to put it on the back burner.

Well thanks in a large part to Marco Shaw, I'm picking this up again. Couple of small things to talk about.

I saw on my Twitter feed recently someone complaining about the load time for Powershell, to go from seeing the window, to the point where you can actually type it. I find that most of these long load times has to deal with a heavily customized profile.

While loading all possible snap-ins, modules, and functions you have written at load time is a good idea in one way, since you have it at hand. It starts to get a little much when you are loading the Exchange Snap-In, PowerCLI for VMWare, and the Quest AD cmd-lets. Loading all three of those will take your load time for Powershell up into the 3 minute mark minimum. Don't even get me started on Exchange 2010 since it uses remoting features and literally downloads the cmd-lets you've been delegated via RBAC.


If your Powershell window is taking a long time to load check what is being loaded in your profile. Type:

PS H:\> $profile
h:\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS H:\>

That will show you the actual ps1 file that Powershell loads up on start. Open that in your favorite text/script editor and clean it up a bit.

To check out what snap-ins you currently have loaded, use the good old:

PS H:\> Get-PSSnapin


Name        : Microsoft.PowerShell.Diagnostics
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.

Name        : Microsoft.WSMan.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that a
              re used by the Windows PowerShell host to manage WSMan operations.

Name        : Microsoft.PowerShell.Core
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

Name        : Microsoft.PowerShell.Utility
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains utility Cmdlets used to manipulate data.

Name        : Microsoft.PowerShell.Host
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are
              provided for use with the Windows PowerShell console host.

Name        : Microsoft.PowerShell.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains management cmdlets used to manage Windows components.

Name        : Microsoft.PowerShell.Security
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Windows PowerShell security.

Name        : Quest.ActiveRoles.ADManagement
PSVersion   : 1.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Active Directory and Quest ActiveRoles Server.

Name        : Microsoft.Exchange.Management.PowerShell.Admin
PSVersion   : 1.0
Description : Admin Tasks for the Exchange Server

As you can see from the above I have two extra snapins loaded, which I did to show some decent output. Decent coding practices state only use what you need, no sense in bloating your environment with stuff your not using.

Finally, a lot of people find Powershell to be fairly daunting as they hear all that you can do with it. Hands down in my opinion the best way to start using Powershell is to simply open it up at your desk, and try some stuff. If there is a task you need to do, say find out what version of Windows a server is using try to find it with Powershell:

PS H:\> Get-WmiObject -Class Win32_OperatingSystem -ComputerName test_server1 | Select-Object Caption
-------
Microsoft Windows Server 2008 R2 Enterprise
PS H:\>

Finally, ask for help. There are tons of forums out there with very knowledgeable people willing to help you out. Check them out.

PS: Anyone on the east coast check out Dev East, particularly in August Marco Shaw(http://marcoshaw.blogspot.com) will be doing a presentation on Powershell, and I'll be doing a short 15 minute one on some real world examples of Powershell use.