{"id":7239,"date":"2014-04-24T07:35:00","date_gmt":"2014-04-24T15:35:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=7239"},"modified":"2014-04-25T11:54:28","modified_gmt":"2014-04-25T19:54:28","slug":"powershell-studio-2014-function-builder","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/04\/24\/powershell-studio-2014-function-builder\/","title":{"rendered":"PowerShell Studio 2014: Function Builder"},"content":{"rendered":"<p>PowerShell Studio 2014 introduces a quick and easy way to create complex functions using the new Function Builder:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image10.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb6.png\" width=\"244\" height=\"149\"><\/a> <\/p>\n<p>Let\u2019s cover each field of the Function Builder and how to create a function.<\/p>\n<p><strong>Cmdlet Name<\/strong><\/p>\n<p>The function builder provides you with a verb and noun field. The verb field contains a combo box with a list of approved verbs, but you have the option to enter your own unapproved verb if necessary.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image11.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb7.png\" width=\"244\" height=\"138\"><\/a><\/p>\n<p><strong>Synopsis &amp; Description<\/strong><\/p>\n<p>You have the option to enter a quick synopsis and a longer description using these fields. The information provided here will be used to generate the function\u2019s comment-based help.<\/p>\n<p><strong>Cmdlet Binding<\/strong><\/p>\n<p>Check the <em>\u201cEnable Cmdlet Binding\u201d<\/em> option if you want your function to behave like a cmdlet and take advantage of PowerShell\u2019s built-in parameters such as <em>Debug <\/em>and <em>Verbose<\/em> and\/or process input from the pipeline. <\/p>\n<p>This option will add the [CmdlingBinding] attribute to your function:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Get-Noun<\/span><span style=\"color: #000000\"> {\r\n    [<\/span><span style=\"color: #4682b4\">CmdletBinding<\/span><span style=\"color: #000000\">(SupportsShouldProcess <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\r\n    <\/span><span style=\"color: #0000ff\">param<\/span><span style=\"color: #000000\"> ()\r\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$pscmdlet<\/span><span style=\"color: #000000\">.ShouldProcess(<\/span><span style=\"color: #ff0000\">\"Target\"<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"Operation\"<\/span><span style=\"color: #000000\">)) {\r\n        \r\n    }\r\n}<\/span><\/pre>\n<p>You can set the cmdlet binding attributes by using the properties grid:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/SNAGHTML14d56dc.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTML14d56dc\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"SNAGHTML14d56dc\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/SNAGHTML14d56dc_thumb.png\" width=\"244\" height=\"143\"><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Tip\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; margin: 10px 4px 0px 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Tip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/Tip1.png\" width=\"32\" align=\"left\" height=\"32\"> <br \/>The property grid provides a help description of each attribute.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Parameter Sets<\/strong><\/p>\n<p>The function builder allows you to specify multiple parameter sets, including a default.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image12.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb8.png\" width=\"244\" height=\"110\"><\/a> <\/p>\n<p>Once you define a parameter set, you can assign it to each individual parameter.<\/p>\n<p><strong>Output Type<\/strong><\/p>\n<p>You can specify the output type of the function and the appropriate attribute will be added: <\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Get-Noun<\/span><span style=\"color: #000000\"> {\r\n    [<\/span><span style=\"color: #4682b4\">CmdletBinding<\/span><span style=\"color: #000000\">()]\r\n    [<\/span><span style=\"color: #4682b4\">OutputType<\/span><span style=\"color: #000000\">([<\/span><span style=\"color: #0000cd\">string<\/span><span style=\"color: #000000\">])]\r\n    <\/span><span style=\"color: #0000ff\">param<\/span><span style=\"color: #000000\"> ()\r\n\r\n}\r\n<\/span><\/pre>\n<p>The output type field also allows you to select from existing types:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image13.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb9.png\" width=\"244\" height=\"191\"><\/a> <\/p>\n<p>The output type field also provides code completion for namespaces and types.<\/p>\n<p><strong>Parameters<\/strong><\/p>\n<p>The function builder makes adding a parameter as simple as typing in a name:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image14.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb10.png\" width=\"244\" height=\"159\"><\/a> <\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<p>The grid of parameters, allows you to mark the parameter as mandatory, provide a name, parameter type and assign a property set. If needed, you can press the <em>Details<\/em> button or press (Ctrl + E) to open the parameter editor for more advanced settings.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Parameter Editor<\/strong><\/p>\n<p>Pressing the <em>New Parameter<\/em> button or the <em>Details<\/em> button will bring up the parameter editor:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image15.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb11.png\" width=\"244\" height=\"169\"><\/a>&nbsp;<\/p>\n<p>Use the parameter editor dialog to edit the details of a specific parameter such as adding validation and aliases. <\/p>\n<p><em><strong>Parameter Name:<\/strong><\/em><\/p>\n<p>Use this field to change the name of the parameter.<\/p>\n<p><em><strong>Description:<\/strong><\/em><\/p>\n<p>The description of the parameter that will appear in the help comment.<\/p>\n<p><strong><em>Alias:<\/em><\/strong><\/p>\n<p>(Optional) Provides an alternative name for the parameter.<\/p>\n<p><strong><em>Type:<\/em><\/strong><\/p>\n<p>Specify the object type of the parameter. Checking the \u201cSwitch Parameter\u201d will make the parameter a switch parameter. This is the same as typing \u201cswitch\u201d in the type field.<\/p>\n<p><em><strong>Settings:<\/strong><\/em><\/p>\n<p>In the settings you set the parameter\u2019s attributes, such as marking the parameter as mandatory.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image16.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb12.png\" width=\"244\" height=\"181\"><\/a> <\/p>\n<p>A helpful description is displayed in when editing a parameter attribute.<\/p>\n<p><strong><em>Validation:<\/em><\/strong><\/p>\n<p>The parameter editor also allows you to apply validation attributes to the parameter. Using the <em>Add<\/em> button you can select and add a validation type and in turn it will be added to the validation list:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image17.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb13.png\" width=\"244\" height=\"135\"><\/a> <\/p>\n<p>Once added, you can modify the validation attribute using the property grid:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image18.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb14.png\" width=\"232\" height=\"244\"><\/a> <\/p>\n<\/p>\n<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image19.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb15.png\" width=\"244\" height=\"160\"><\/a> <\/p>\n<p><strong><em>Parameter Set:<\/em><\/strong><\/p>\n<p>You can also set the parameter\u2019s parameter set assignment with in the parameter editor:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image20.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb16.png\" width=\"244\" height=\"64\"><\/a> <\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>Once you have added all the parameters and entered the information required, press the <em>OK<\/em> button in the Function Builder dialog. Now you have an advanced function ready to go:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image21.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/04\/image_thumb17.png\" width=\"244\" height=\"134\"><\/a><\/p>\n<p>The function builder is a time saver and a learning tool for those who may not be familiar with the intricacies of creating an advanced function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Studio 2014 introduces a quick and easy way to create complex functions using the new Function Builder: Let\u2019s cover each field of the Function Builder and how to create a function. Cmdlet Name The function builder provides you with a verb and noun field. The verb field contains a combo box with a list [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[703,632,25],"tags":[909,28,874,141],"class_list":["post-7239","post","type-post","status-publish","format-standard","hentry","category-powershell-studio","category-whats-new-software-news","category-windows-powershell","tag-function-builder","tag-powershell","tag-powershell-studio-2014","tag-whats-new"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=7239"}],"version-history":[{"count":5,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7239\/revisions"}],"predecessor-version":[{"id":7308,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7239\/revisions\/7308"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=7239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=7239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=7239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}