I’m very happy to see more and more people getting their hands dirty with Windows PowerShell. A common challenge I see across different support forums is getting information from one part of a script to another. Very often the user has created a function and is attempting to use its output elsewhere in their script. Often I can tell the scripter is coming from a VBScript background or at least thinking that way. There’s nothing necessarily wrong with writing a PowerShell script in a VBScript style, but these scripts are missing out. Let me walk you through several iterations of a function to demonstrate.
Tag: scripting
PowerShell Special Forces Invades Columbus, Ohio
Are you ready Ohio? We’re bringing our intense, one-of-a-kind Special Forces PowerShell training to Columbus, Ohio July 27-29, 2009. We’re looking for up to 15 recruits who want to learn PowerShell from the ground up and aren’t afraid to get their hands duty. This is unlike any other training course you’ve ever taken. Not everyone can meet the requirements, but those that do learn more PowerShell than they realized was possible in three days. But wait..there’s more!
Can I Get a Little Help Here?
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.
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.
Registering PrimalPad as your default editor
Associating a file type with a specific application has always been fairly easy to do under the various incarnations of Windows. Basically, you can right-click on a file of the type you wish to use, select the Open With | Choose Program context menu option, then select the program like PrimalPad from the list or […]
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.
VBScript Complex Regex Replace
A few days ago I posted a blog entry on simple regular expression replacements in VBScript. Let me show you a more complex example. It helps to have a purpose, even for demonstration so my need is to convert an html table to CSV output using regular expressions.