{"id":7495,"date":"2014-07-09T09:41:00","date_gmt":"2014-07-09T17:41:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=7495"},"modified":"2014-07-09T09:42:10","modified_gmt":"2014-07-09T17:42:10","slug":"powershell-studio-2014-convert-functions-into-a-module","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/07\/09\/powershell-studio-2014-convert-functions-into-a-module\/","title":{"rendered":"PowerShell Studio 2014: Convert Functions into a Module"},"content":{"rendered":"<p>PowerShell Studio 2014 added a new project feature: <em>New Module From Functions<\/em>.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/image.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\/07\/image_thumb.png\" width=\"214\" height=\"244\"><\/a><\/p>\n<p>You may find at times you reuse various functions and constantly copy them into new scripts or find different variations of the same function are located in different scripts. You may come to the conclusion that those functions would better serve you in the form of a module where all your scripts can reference the functions in a centralized location. This is where the <em>New Module From Functions<\/em> feature comes in handy.<\/p>\n<p>The <em>New Module From Functions<\/em> allows you to import functions from various ps1 scripts and merge them into a new script module. When you select this project from the menu it will ask you to select a file (if one not already open) and present you with the following dialog:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/SNAGHTML330af9.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTML330af9\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"SNAGHTML330af9\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/SNAGHTML330af9_thumb.png\" width=\"244\" height=\"163\"><\/a><\/p>\n<p>Let\u2019s cover each field individually:<\/p>\n<p><em><strong>Module Name<\/strong><\/em><\/p>\n<p>Enter the name you wish to give your new module.<\/p>\n<p><em><strong>Location<\/strong><\/em><\/p>\n<p>Specify the folder where the module will be saved. The default is PowerShell Studio\u2019s project directory.<\/p>\n<p><em><strong>Create Module Folder<\/strong><\/em><\/p>\n<p>This option creates a folder using the module\u2019s name and places all the generated files within that folder. This folder will be created in the folder specified by the <em>Location<\/em> field.<\/p>\n<p><em><strong>Source Files<\/strong><\/em><\/p>\n<p>Contains the list of files from which the selected functions with be extracted.<\/p>\n<p><strong><em>Add File<\/em><\/strong><\/p>\n<p>Use the <em>Add File<\/em> button to add ps1 scripts to the <em>Source Files<\/em>, in order to extract their functions.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/SNAGHTML3d6f18.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTML3d6f18\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"SNAGHTML3d6f18\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/SNAGHTML3d6f18_thumb.png\" width=\"244\" height=\"134\"><\/a><\/p>\n<p><strong><em>Remove File<\/em><\/strong><\/p>\n<p>Use the <em>Remove File<\/em> button to remove unnecessary files from the <em>Source Files<\/em> list. This can help de-clutter the functions list.<\/p>\n<p><strong><em>Functions<\/em><\/strong><\/p>\n<p>The functions section contains a node for each file and its respective list of functions that are declared in said file.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/image1.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\/07\/image_thumb1.png\" width=\"244\" height=\"168\"><\/a><\/p>\n<p>Use this list to check the functions you wish to import into the new module. If a function references another function, it will have a <em>Referenced Functions<\/em> folder icon below that contains a list of the all the referenced functions.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/image2.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\/07\/image_thumb2.png\" width=\"244\" height=\"73\"><\/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 3px 0px 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Tip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/Tip.png\" width=\"32\" align=\"left\" height=\"32\"><br \/>When you check a function in the list that has references, it will automatically check all the referenced functions as well.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Tip\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; margin: 0px 3px 5px 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Tip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/Tip.png\" width=\"32\" align=\"left\" height=\"32\"> You can check and un-check all the functions in the file by checking\/un-checking the file\u2019s node.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>Create Module<\/em><\/strong><\/p>\n<p>Once you have selected the functions and specified a module name you can press the <em>Create Module<\/em> button to generate the new module.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/image3.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\/07\/image_thumb3.png\" width=\"244\" height=\"151\"><\/a><\/p>\n<p><em>Note on duplicate functions:<\/em><br \/>In some instances you may have a duplicate function that is defined in multiple files. PowerShell Studio will compare these functions and if they are identical, it will only insert the function once. If the functions are different, PowerShell Studio will automatically rename the duplicate. A warning will be displayed in the Output window when this occurs:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/07\/image4.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\/07\/image_thumb4.png\" width=\"644\" height=\"171\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The new module project will contain three files:<\/p>\n<p><em>Module Name.psd1<\/em> \u2013 Contains the module\u2019s manifest.<\/p>\n<p><em>Module Name.psm1<\/em> \u2013 Contains all the imported functions and an Export-ModuleMember cmdlet statement listing all the imported functions.<\/p>\n<p><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\/07\/image5.png\" width=\"320\" height=\"177\"><\/p>\n<p>You can remove any function you do not wish to be exported by the module here.<\/p>\n<p><em>Module Name.Help.xml<\/em> \u2013 Contains the PowerShell Xml Help file for the module, generated using the imported functions.<\/p>\n<p>&nbsp;<\/p>\n<p>Now you have a complete PowerShell module with minimal effort on your part.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Studio 2014 added a new project feature: New Module From Functions. You may find at times you reuse various functions and constantly copy them into new scripts or find different variations of the same function are located in different scripts. You may come to the conclusion that those functions would better serve you in [&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,21,632],"tags":[349,282,753,28,874],"class_list":["post-7495","post","type-post","status-publish","format-standard","hentry","category-powershell-studio","category-software-news","category-whats-new-software-news","tag-convert","tag-functions","tag-module","tag-powershell","tag-powershell-studio-2014"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7495","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=7495"}],"version-history":[{"count":10,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7495\/revisions"}],"predecessor-version":[{"id":7507,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7495\/revisions\/7507"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=7495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=7495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=7495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}