Book Image

PowerShell Troubleshooting Guide

By : Mike Shepard
Book Image

PowerShell Troubleshooting Guide

By: Mike Shepard

Overview of this book

Table of Contents (15 chapters)
PowerShell Troubleshooting Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Parameter type transformation


PowerShell allows us to ignore the idea of variable types in most situations and this is a tremendous productivity boost. When you consider all of the different Common Language Runtime (CLR) types that are used in a typical script it's easy to see why not worrying about naming the types saves a lot of time. Adding in all of the anonymous types (for instance, results of a select-object call), the need for a very liberal typing system is obvious. One example when specifying types is useful, or even critical, is when specifying parameters to a function:

function get-dayofweek{
param($date)
  Write-Output $date.DayOfWeek
}

This function seems like it would work well, but testing it shows that it's not quite right, as shown in the following screenshot:

Since we didn't specify what type the parameter was, the problem was that the constant string '5/27/2014' was passed into the parameter as is, that is, as a string. Since the string didn't have a DayOfWeek property, the...