{"id":10631,"date":"2006-04-25T12:18:09","date_gmt":"2006-04-25T12:18:09","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/ensuring-script-portability-with-requires\/"},"modified":"2019-02-18T13:24:56","modified_gmt":"2019-02-18T20:24:56","slug":"ensuring-script-portability-with-requires","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/ensuring-script-portability-with-requires\/","title":{"rendered":"Ensuring Script Portability with #requires"},"content":{"rendered":"<p><font face=\"Arial\">With the MshSnapIn model in Beta 3, there is no gaurantee that a certain Cmdlet or Provider,&nbsp;apart from those in the default&nbsp;MshSnapIns,&nbsp;is available at any point. There could be a number of reasons. The MshSnapIn&nbsp;may not have been installed.&nbsp;The MshSnapIn&nbsp;could&nbsp;be removed with the remove-mshsnapin Cmdlet or&nbsp;not even added yet.&nbsp;This will cause scripts using the unavailable commands to fail or worse exhibit unpredictable&nbsp;behaviors. Along with the MshSnapIn model, we introduce two flavors of #requires that a script can use to express dependencies on MshSnapIns and&nbsp;Monad&nbsp;version<\/font><font face=\"Arial\">.&nbsp;The&nbsp;former has the following&nbsp;syntax:<\/font><\/p>\n<p><font face=\"Arial\"><\/p>\n<p><font face=\"Arial\"><font face=\"Courier New\">#requires -MshSnapIn <em>SomeMshSnapIn<\/em> [-version <em>N<\/em>[<em>.n<\/em>]]<\/font>&nbsp; where the parts in square brackets are optional. #requires must be at the beginning of a line.<\/font><\/p>\n<p>When only MshSnapIn is specified, the script will run if&nbsp;and only if the MshSnapIn&nbsp;<em><font face=\"Courier New\">SomeMshSnapIn<\/font><\/em>&nbsp;is already added with the Add-MshSnapIn Cmdlet (or the RunspaceConfiguration.AddMshSnapIn API).&nbsp;Moreover, if the major version <em>N<\/em> is specified, the added MshSnapin&#8217;s major version must match <em>N<\/em>. Furthermore, if the minor version <em>n<\/em> is also specified, the added MshSnapin&#8217;s minor version must be equal to or higher than <em>n<\/em>.&nbsp;Failing any&nbsp;of the checks will cause the script not to run. A script can have multiple&nbsp;#requires -MshSnapIn directives. In this case, all MshSnapIn checks will need to pass for the script to run.<\/p>\n<p>The latter&#8217;s syntax is as follows. <\/p>\n<p><font face=\"Arial\"><font face=\"Courier New\">#requires -version <em>N<\/em>[<em>.n<\/em>]<\/font>&nbsp; where the parts in square brackets are optional. #requires must be at the beginning of a line.<\/font><\/p>\n<p>When only the major version <em>N<\/em> is specified, the current Monad shell&#8217;s version must match <em>N<\/em>. If the minor version is also&nbsp;<em>n<\/em> specified, the current Monad shell&#8217;s minor version must be equal to or higher than <em>n<\/em>. Failing any&nbsp;of the&nbsp;checks will cause the script not to run.<\/p>\n<p>Finally, for&nbsp;the custom shell environment, the #requires -ShellId flavor is still available. A script can have any combinations of the three #requires flavors.&nbsp;#requires -version will check the Monad shell version for all combinations.&nbsp;As for the other two flavors, here is the behavior summary:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td vAlign=\"top\" width=\"300\">If a script has<\/td>\n<td vAlign=\"top\" width=\"150\">Monad Shell<\/td>\n<td vAlign=\"top\" width=\"100\">Custom Shell<\/td>\n<\/tr>\n<tr>\n<td>#requires -MshSnapIn and no #requires -ShellId<\/td>\n<td>Check MshSnapIn<\/td>\n<td>Script won\u2019t run<\/td>\n<\/tr>\n<tr>\n<td>#requires -ShellId and no #requires -MshSnapIn<\/td>\n<td>Script won\u2019t run<\/td>\n<td>Check ShellId<\/td>\n<\/tr>\n<tr>\n<td>#requires -ShellId and #requires -MshSnapIn<\/td>\n<td>Check MshSnapIn<\/td>\n<td>Check ShellId<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;<br \/>Kevin Loo [MSFT]<br \/>Monad Development<br \/>Microsoft Corporation<br \/>This posting is provided &#8220;AS IS&#8221; with no warranties, and confers no rights.<\/p>\n<p><\/font><\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the MshSnapIn model in Beta 3, there is no gaurantee that a certain Cmdlet or Provider,&nbsp;apart from those in the default&nbsp;MshSnapIns,&nbsp;is available at any point. There could be a number of reasons. The MshSnapIn&nbsp;may not have been installed.&nbsp;The MshSnapIn&nbsp;could&nbsp;be removed with the remove-mshsnapin Cmdlet or&nbsp;not even added yet.&nbsp;This will cause scripts using the unavailable [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>With the MshSnapIn model in Beta 3, there is no gaurantee that a certain Cmdlet or Provider,&nbsp;apart from those in the default&nbsp;MshSnapIns,&nbsp;is available at any point. There could be a number of reasons. The MshSnapIn&nbsp;may not have been installed.&nbsp;The MshSnapIn&nbsp;could&nbsp;be removed with the remove-mshsnapin Cmdlet or&nbsp;not even added yet.&nbsp;This will cause scripts using the unavailable [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10631"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10631\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}