I’ve been writing about the different parameter validation attributes that you can use in your PowerShell scripting. One that I use in practically every script is [ValidateNotNullorEmpty()]. This validation will ensure that something is passed as a parameter value. I’m not talking about making a parameter mandatory; only that if the user decides to use…
Tag: PowerShell
SQL Saturday 129 Session Material
I spoke this past weekend at a SQL Saturday event in Rochester, NY. My first SQL Saturday event and it was a lot of fun. A great turnout and some very interested attendees. I did three PowerShell sessions on jobs, scheduled jobs/tasks and an intro to workflows. The latter talk I think blew out some…
Friday Fun: PowerShell ISE Function Finder
At the PowerShell Deep Dive in San Diego, I did a lightning session showing off something I had been working on. Sometimes I don’t know what possesses me, but I felt the need for a better way to navigate my PowerShell scripts files that had many functions. Some files, especially modules, can get quite long…
Quick and Dirty Excel from PowerShell
I continue to tinker with Office applications and Windows PowerShell. I was looking at an Excel issue related to opening a new workbook. To verify the problem wasn’t PowerShell related I offered a suggestion to try creating an Excel workbook in VBScript. In VBScript creating a blank workbook in Microsoft Excel can be accomplished with…
San Diego 2012 PowerShell Deep Dive Slides and Demos
Last week at the PowerShell Deep Dive in San Diego, I did a short presentation on integrating Microsoft Office applications like Excel and Word with Windows PowerShell. I easily could have spoken much longer and probably tried to cram too much in. I spent a lot of time with my demos. I expect at some…
PowerShell Scripting with [ValidateCount]
Here’s another parameter validation attribute you might want to use in your PowerShell scripting and functions. If your parameter can take an array of values, you might want to limit that array to a certain size. For example, your parameter can take an array of computer names but you don’t want to process more than…
Skipping WMI System Properties in PowerShell
One of my favorite techniques when using WMI in PowerShell is to pipe an object to Select-Object and select all properties. Try this: get-wmiobject win32_bios | select * It works, but it also gets all of the system properties like __PATH which I rarely care about. I also get other properties like Site and Options…
Convert Boolean Values
First, let me state right off the bat that what I have here should be for very special use cases and is NOT something I feel you need to be using at all. Now the use case: You are preparing a report of some sort for a non-technical user and instead of displaying True or…
PowerShell Scripting with [ValidateLength]
In continuing the exploration of parameter validation attributes, today we’ll look at [ValidateLength()]. You can use this attribute in your PowerShell scripting to validate that a parameter value is at least a certain length and no more and a certain length. In other words, it has to be just right. Here’s what it looks like:…
PowerShell Scripting with [ValidatePattern]
I’ve been writing about a number of parameters attributes you can include in your PowerShell scripting to validate parameter values. Today I want to cover using a regular expression pattern to validate a parameter value. I’m going to assume you have a rudimentary knowledge of how to use regular expressions in PowerShell. If not, there…
PowerShell Scripting with [ValidateSet]
Today we’ll continue our exploration of the parameter validation attributes you can use in you PowerShell scripting. We’ve already looked at [ValidateRange] and [ValidateScript]. Another attribute you are likely to use is [ValidateSet()]. You can use this to verify that the parameter value belongs to a pre-defined set. To use, specify a comma separated list…
Friday Fun: 13 More Scriptblocks
In celebration of Friday the 13th I thought I would offer up a menu of 13 more script blocks. If you missed the first course, you can find the original 13 scrptblocks here. I’m not going to spend a lot of time going over these. Many of them are simple one liners. Some of them…
PowerShell Scripting with [ValidateScript]
The last few days we’ve been looking at parameter validation attributes you might use in a script of function. Yesterday I wrote about [ValidateRange] and demonstrated how you might use it. That attribute works fine for any values that can be evaluated as numbers. But dates are a different story. I got a comment with…
PowerShell Scripting with [ValidateRange]
After my post yesterday on using the ValidateScript attribute with PSCredentials, I thought you might find it helpful to have a brief discussion on some other parameter validation attributes such as [ValidateRange()]. You can use this attribute if you want to verify that a given parameter value falls between some range. Typically this is used…
Scripting with PSCredential
I see this question often: how can I pass a parameter value for a PSCredential that might be a credential object or it might be a user name? In the past I’ve used code like this: begin { Write-Verbose -Message “Starting $($myinvocation.mycommand)” write-verbose -Message “Using volume $($volume.toUpper())” #convert credential to a PSCredential if a string…