{"id":4876,"date":"2012-10-03T00:01:00","date_gmt":"2012-10-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/03\/build-your-own-powershell-cmdlet-part-5-of-9\/"},"modified":"2012-10-03T00:01:00","modified_gmt":"2012-10-03T00:01:00","slug":"build-your-own-powershell-cmdlet-part-5-of-9","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-your-own-powershell-cmdlet-part-5-of-9\/","title":{"rendered":"Build Your Own PowerShell Cmdlet: Part 5 of 9"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs detailing building your own cmdlet.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/sean+kearney\/\" target=\"_blank\">his previous guest blog posts<\/a>.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> This is Part 5 of a nine-part series about building your own Windows PowerShell cmdlet. Read the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/guest+blogger\/sean+kearney\/build+your+own+cmdlet\/\">entire series<\/a> as it unfolds.<\/p>\n<p>Here&rsquo;s Sean&hellip;<\/p>\n<p>Yesterday, we introduced some common parameters that are now available to our cmdlet:<\/p>\n<ul>\n<li>\n<p>-Verbose<\/p>\n<\/li>\n<li>\n<p>-Debug<\/p>\n<\/li>\n<li>\n<p>-WarningAction<\/p>\n<\/li>\n<li>\n<p>-WarningVariable<\/p>\n<\/li>\n<li>\n<p>-ErrorAction<\/p>\n<\/li>\n<li>\n<p>-ErrorVariable<\/p>\n<\/li>\n<li>\n<p>-OutVariable<\/p>\n<\/li>\n<li>\n<p>-OutBuffer<\/p>\n<\/li>\n<\/ul>\n<p>Now we can modify our existing advanced function to leverage some of these parameters.<\/p>\n<p>We are already using the <b>Write-Error<\/b> cmdlet. We can also add <b>Write-Debug<\/b> in various points of the script to show the values of our variables for troubleshooting.<\/p>\n<p style=\"padding-left: 30px\">function global:ADD-LOGFILE{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">[CmdletBinding(<\/p>\n<p style=\"padding-left: 30px\">DefaultParameterSetName=&rdquo;Folder&rdquo;,<\/p>\n<p style=\"padding-left: 30px\">SupportsShouldProcess=$True,<\/p>\n<p style=\"padding-left: 30px\">ConfirmImpact=&rsquo;High&rsquo;<\/p>\n<p style=\"padding-left: 30px\">)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PARAM(<\/p>\n<p style=\"padding-left: 30px\">[STRING[]]$Folder=&#8221;C:\\PowerShell&#8221;,<\/p>\n<p style=\"padding-left: 30px\">[STRING[]]$Preface=&#8221;Logfile&#8221;,<\/p>\n<p style=\"padding-left: 30px\">[STRING[]]$Extension=&#8221;.log&#8221;<\/p>\n<p style=\"padding-left: 30px\">)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Folder: $Folder&rdquo;<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Preface: $Preface&rdquo;<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Extension: $Extension&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># GET the Current Date for our Logfile<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Today=GET-DATE<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Today: $Today&rdquo;<\/p>\n<p style=\"padding-left: 30px\"># Extract the Date removing the &ldquo;\/&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Date=$Today.toshortdatestring().Replace(&ldquo;\/&rdquo;,&rdquo;&rdquo;)<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Date: $Date&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Extract the Time removing the &ldquo;:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Time=$Today.tostring(&ldquo;HH:mm:ss&rdquo;).Replace(&ldquo;:&rdquo;,&rdquo;&ldquo;)<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Time: $Time&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Build our Filename<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Logfilename=$Folder+&#8221;\\&#8221;+$Preface+&rdquo;-&ldquo;+$Date+&rdquo;-&ldquo;+$Time+$Extension<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;`$Logfilename: $Logfilename&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Test and ensure file does not already exist<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">IF (TEST-PATH -path $Logfilename)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">{ WRITE-ERROR &ndash;message &ldquo;Error: $Logfilename exists.&rdquo; &ndash;category &lsquo;WriteError&rsquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># If file exists, return a status of Boolean $False for Unsuccessful<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">RETURN $Logfilename,$FALSE }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">ELSE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Create logfile<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">NEW-ITEM &ndash;Type File -path $Logfilename -Force | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">WRITE-DEBUG &ldquo;$Logfilename successfully created&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Return the Full path and filename if successful<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">RETURN $Logfilename,$TRUE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>We&rsquo;ll save this as a new Windows PowerShell script called addlogcmdlet.ps1 and execute it to create the new cmdlet in memory.<\/p>\n<p>With these new changes to our cmdlet, we can troubleshoot and view the variables within the cmdlet (or whatever output we choose reveal) by adding the <b>Verbose<\/b> parameter.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2234.hsg-10-3-12-1.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2234.hsg-10-3-12-1.png\" \/><\/a><\/p>\n<p>We can even have our cmdlet pause and ask us what to do if an error occurs by leveraging the <b>ErrorAction <\/b>parameter.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0488.hsg-10-3-12-2.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0488.hsg-10-3-12-2.png\" \/><\/a><\/p>\n<p>~Sean<\/p>\n<p>Thank you, Sean. The cmdlet is really coming together. Nice additions today. Guest Blogger Week will continue tomorrow when Sean will talk more about building a cmdlet.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs detailing building your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see his previous guest blog posts. Note This is Part 5 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[372,56,3,154,45],"class_list":["post-4876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-build-your-own-cmdlet","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs detailing building your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see his previous guest blog posts. Note This is Part 5 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4876"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4876\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}