{"id":1103,"date":"2008-04-07T02:46:35","date_gmt":"2008-04-07T02:46:35","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/heaths\/2008\/04\/07\/functional-testing-of-cmdlets\/"},"modified":"2019-02-17T15:30:03","modified_gmt":"2019-02-17T22:30:03","slug":"functional-testing-of-cmdlets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/setup\/functional-testing-of-cmdlets\/","title":{"rendered":"Functional Testing of Cmdlets"},"content":{"rendered":"<p>While developing unit and functional tests for <a href=\"http:\/\/www.codeplex.com\/psmsi\">Windows Installer PowerShell Extensions<\/a>, I needed a way to invoke <a href=\"http:\/\/msdn2.microsoft.com\/library\/ms714395.aspx\">cmdlets<\/a> without requiring elevation on Vista. That is, of course, because running elevated has always been a bad idea unless it is required. In order to load a <a href=\"http:\/\/msdn2.microsoft.com\/library\/ms714425.aspx\">PowerShell snap-in<\/a>, however, one must write to HKEY_LOCAL_MACHINE which requires elevated privileges. Other than that, there really isn&#8217;t another reason to run elevated.<\/p>\n<p>Fortunately, PowerShell allows developers to define a <a href=\"http:\/\/msdn2.microsoft.com\/library\/system.management.automation.runspaces.runspaceconfiguration.aspx\">RunspaceConfiguration<\/a> object which, among other properties, allows developers to add specific types as cmdlets. Without being defined by a registered snap-in, cmdlets can then be invoked in a <a href=\"http:\/\/msdn2.microsoft.com\/library\/system.management.automation.runspaces.runspace.aspx\">Runspace<\/a> as shown below in a sample <a href=\"http:\/\/msdn2.microsoft.com\/library\/ms182449.aspx\">Visual Studio test class<\/a>.<\/p>\n<pre class=\"code\"><span>using<\/span> System;\n<span>using<\/span> System.Management.Automation;\n<span>using<\/span> System.Management.Automation.Runspaces;\n<span>using<\/span> Microsoft.Windows.Installer.PowerShell.Commands;\n<span>using<\/span> Microsoft.VisualStudio.TestTools.UnitTesting;\n[TestClass]\n<span>public<\/span> <span>class<\/span> <span>GetFileHashCommandTest\n<\/span>{\n    <span>private<\/span> RunspaceConfiguration config;\n    [TestInitialize]\n    <span>public<\/span> <span>void<\/span> Initialize()\n    {\n        config = RunspaceConfiguration.Create();\n        config.Cmdlets.Append(<span>new<\/span> CmdletConfigurationEntry(\n            <span>\"Get-MSIFileHash\"<\/span>,\n            <span>typeof<\/span>(GetFileHashCommand),\n            <span>\"Microsoft.Windows.Installer.PowerShell.dll-Help.xml\"<\/span>));\n    }\n    [TestMethod]\n    [DeploymentItem(<span>@\"data\\example.txt\"<\/span>)]\n    <span>public<\/span> <span>void<\/span> PathTest()\n    {\n        <span>using<\/span> (Runspace rs = RunspaceFactory.CreateRunspace(config))\n        {\n            rs.Open();\n            <span>using<\/span> (Pipeline p = rs.CreatePipeline(<span>@\"get-msifilehash -path example.txt\"<\/span>))\n            {\n                Collection&lt;PSObject&gt; objs = p.Invoke();\n                Assert.AreEqual&lt;<span>int<\/span>&gt;(1, objs.Count);\n            }\n        }\n    }\n}<\/pre>\n<p>When it is time to install and register your PowerShell snap-in, I recommend you take a look at the <a href=\"http:\/\/blogs.msdn.com\/heaths\/archive\/2007\/05\/18\/wix-extension-for-powershell-snap-ins.aspx\">WiX PowerShell extension<\/a> to <a href=\"http:\/\/robmensching.com\/blog\/archive\/2007\/04\/19\/Managed-Code-CustomActions-no-support-on-the-way-and-heres.aspx\">avoid using managed custom<\/a> actions which can also cause problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While developing unit and functional tests for Windows Installer PowerShell Extensions, I needed a way to invoke cmdlets without requiring elevation on Vista. That is, of course, because running elevated has always been a bad idea unless it is required. In order to load a PowerShell snap-in, however, one must write to HKEY_LOCAL_MACHINE which requires [&hellip;]<\/p>\n","protected":false},"author":389,"featured_media":3843,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,14,30,39,45],"class_list":["post-1103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-net","tag-development","tag-powershell","tag-tip","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>While developing unit and functional tests for Windows Installer PowerShell Extensions, I needed a way to invoke cmdlets without requiring elevation on Vista. That is, of course, because running elevated has always been a bad idea unless it is required. In order to load a PowerShell snap-in, however, one must write to HKEY_LOCAL_MACHINE which requires [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/users\/389"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/comments?post=1103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/1103\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media\/3843"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media?parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/categories?post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/tags?post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}