One of my favorite features in PowerShell v2.0 is the ability to add help scripts and functions. The help system with cmdlets is terrific. Type help and the name of a cmdlet and you get syntax, parameters, a description and examples. In PowerShell v2 you can get this same functionality for your scripts and functions. The example I want to show you will work with PowerShell v2.0 that ships with WIndows 7 Release Candidate. There have been some changes since PowerShell CTP3 so if you want the absolute latest PowerShell bits you’ll need Windows 7. Here’s what you can look forward to.
Category: Windows PowerShell
Out-CSV Revised
A while ago I published a short function to create CSV strings. While working on another task I came across $OFS, which is something I know about but never really think about. $OFS is an intrinsic PowerShell variable that controls the output field separator for arrays. You can modify this variable which made me realize there was a better way to handle my Out-CSV function. Here’s the revised function.
VMware Infrastructure book now available as eBook
Hal Rottenberg’s Managing VMware Infrastructure with Windows PowerShell is now available as PDF eBook on ScriptingOutpost.com. It is also available as a print + eBook combo. Check out Hal’s book and all of the other SAPIENPress books available on ScriptingOutpost.com.
A PowerShell Port Scan
I recently guest-hosted Episode 69 the PowerScripting Podcast. One of the items we discussed was a port scanning script from TheAdminGuy blog. Here’s the original script: http://theadminguy.wordpress.com/2009/04/30/portscan-with-powershell/. As written, there’s nothing technically wrong with it. The script gets the job done and tells the user which ports are open. However, I thought this was an opportunity to demonstrate an even more PowerShell-like approach. Even the author admits there are limitations to his script.
Whenever I see a script like this, the first thing I try to do is separate the functionality. In the VBScript days we would write monolithic scripts to do everything. Sure, we might write a function or subroutine, but based on my forum experiences, even that was not a common occurrence. In PowerShell, we want to use smaller, cmdlet-like tools that leverage the pipeline.
Sometimes an Apple is an Orange
I was helping a member out in the PowerShell forum at ScriptingAnswers.com. He was trying to create a multistring registry entry on a remote computer using the .NET registry classes. Creating registry values is really not that complicated. Here’s a short example.
May PowerShell Oneliner
In case you missed this recent SAPIEN newsletter, here’s my one liner for this month. I have a quick one-liner that will create a random filename with a preferred file extension in a folder that you specify. By default the GetRandomFileName() method will return a short 8.3 string with a random file name and extension. […]
Cool Config Creation
In my recent Special Forces PowerShell class, one of the students brought up a challenge he was facing when we were talking about the -f replacement operator. This is a handy way to build strings.
PS C:\> “Hello, {0}. It is now {1} and you have {2} processes running.” -f $env:username,(get-date).ToShortDateString(),
(get-process | measure-object).count
Hello, Jeff. It is now 5/5/2009 and you have 82 processes running.
Paul needed to build configuration files and was looking for an easy way, perhaps using a here string as a template. We tossed around a few ideas. When I got home I decided to see what I could come up with. What I have is a proof of concept really, but it might get you started.
The Mission Will Start Without You
I’ll be in Boston next week running our Special Forces PowerShell training course. There’s still room for a few recruits on this mission, but you absolutely must register by April 24th. Sooner is actually better to make sure your training equipment arrives in time. Don’t put this off any longer. Register online at http://www.scriptingoutpost.com/p-81-special-forces-training-windows-powershell.aspx.
Fun with PowerShell ISE
A popular PowerShell version 2 feature is the graphical PowerShell console, known as the Integrated Script Environment or ISE. The ISE has its own object model which means you can customize it. Here a few things you might want to tweak, or at least experiment with.
Get Local Member
I’ve been dealing with the topic of enumerating local group membership in several PowerShell forums from different people. My responses have pretty much been the same. But if these people are asking its a safe bet there are others who are also in need. The bottom line has been to query a local group, typically Administrators, and return a list of group members. If the member is a domain account, then get a little extra information. I put together a script called Get-Localmember.ps1 that I think will get the job done for most of you.