Dot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.The HyperV guys have a nice hyperv.ps1 script that contains definitions for cmdlets like (Get-VM, Get-VMHost etc. etc.). <div><br/></div> <div>So in the PowerShell command line i dot source (. ./hyperv.ps1) then run 'Get-VM' and all is well.</div> <div><br/></div> <div>I'd like to do the same from .Net:</div> <div><br/></div> <div> <div></div> <pre lang="x-c#">string scriptContents = string.Empty; using(StreamReader fs = new StreamReader(@&quot;hyperv.ps1&quot;)) {   scriptContents = fs.ReadToEnd(); } . . . . pipeline.Commands.AddScript(&quot;scriptContents&quot;); pipeline.Command.Add(new Command(&quot;Get-VM&quot;)); pipeline.Invoke(); </pre> Unfortunately I get a CommandNOtFoundException :&quot;The term 'Get-VMHost' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.&quot; <br/> <div><br/></div> <div>Thanks for your help in advance.</div> <div><br/></div> <div>Matt</div> <div><br/></div> <div><br/></div> <div><br/></div> <div><br/></div> </div>© 2009 Microsoft Corporation. All rights reserved.Tue, 30 Jun 2009 10:37:40 Z32528df8-cb9a-4b2c-b5a8-d39f7fe9df0fhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#32528df8-cb9a-4b2c-b5a8-d39f7fe9df0fhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#32528df8-cb9a-4b2c-b5a8-d39f7fe9df0flushdoghttp://social.technet.microsoft.com/Profile/en-US/?user=lushdogDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.The HyperV guys have a nice hyperv.ps1 script that contains definitions for cmdlets like (Get-VM, Get-VMHost etc. etc.). <div><br/></div> <div>So in the PowerShell command line i dot source (. ./hyperv.ps1) then run 'Get-VM' and all is well.</div> <div><br/></div> <div>I'd like to do the same from .Net:</div> <div><br/></div> <div> <div></div> <pre lang="x-c#">string scriptContents = string.Empty; using(StreamReader fs = new StreamReader(@&quot;hyperv.ps1&quot;)) {   scriptContents = fs.ReadToEnd(); } . . . . pipeline.Commands.AddScript(&quot;scriptContents&quot;); pipeline.Command.Add(new Command(&quot;Get-VM&quot;)); pipeline.Invoke(); </pre> Unfortunately I get a CommandNOtFoundException :&quot;The term 'Get-VMHost' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.&quot; <br/> <div><br/></div> <div>Thanks for your help in advance.</div> <div><br/></div> <div>Matt</div> <div><br/></div> <div><br/></div> <div><br/></div> <div><br/></div> </div>Thu, 25 Jun 2009 18:04:32 Z2009-06-25T18:04:32Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#c489c13d-f1bd-4013-9bf2-a86d273ec2d6http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#c489c13d-f1bd-4013-9bf2-a86d273ec2d6Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.That's a good one.<br/> <br/> Since this is a runspace, I think I'd try this first by modifying the function in hyper.ps1:<br/> <br/> Instead of:<br/> function get-vm {...<br/> }<br/> <br/> Change it to:<br/> function global:get-vm {...<br/> }<br/> <br/> Then when you load hyper.ps1, I think you will have access to the function in your current scope.<br/> <br/> I don't know what might be the implications of changing *all* the functions to be in the global scope in this case.Thu, 25 Jun 2009 18:31:22 Z2009-06-25T18:31:22Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#d58fc963-6c24-4432-9e96-67669ec70e19http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#d58fc963-6c24-4432-9e96-67669ec70e19lushdoghttp://social.technet.microsoft.com/Profile/en-US/?user=lushdogDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.I figured out that i should be loading the script in the runspace rather than the pipeline: <div><br/></div> <div></div> <pre lang="x-c#">myRunspaceInvoke.Invoke(&quot;. ./hyperv.ps1&quot;); </pre> <div><br/></div> <div>Unfortunately I get a &quot;AuthorizationManager check failed.&quot; exception as I believe the script is not signed or some other authentication problem.</div>Thu, 25 Jun 2009 22:42:07 Z2009-06-25T22:42:07Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#d6161456-0330-4db4-9a1d-20396349d8d8http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#d6161456-0330-4db4-9a1d-20396349d8d8Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.Are you using the new v2 CTP3 here?<br/> <br/> Known problem:<br/> https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=466285&amp;SiteID=99<br/>Fri, 26 Jun 2009 13:38:47 Z2009-06-26T13:38:47Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#3911304f-4ebe-464f-a607-80d6c590dae2http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#3911304f-4ebe-464f-a607-80d6c590dae2lushdoghttp://social.technet.microsoft.com/Profile/en-US/?user=lushdogDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.Nope I'm using Powershell 1.0.<br/> <br/> I can't seem to find that feedback item you've linked.<br/> <br/> Can you cut and paste the summary here so I can read it over?Fri, 26 Jun 2009 17:17:36 Z2009-06-26T17:17:36Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#90485f03-35f6-4e02-b18c-e19b164b60b9http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#90485f03-35f6-4e02-b18c-e19b164b60b9Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.I'll have to try some more testing with runspaces.  This may be related directly to WMI though...<br/> <br/> When the WMI service (winmgmt) is &quot;paused&quot;, the policy execution equal to &quot;AllSigned&quot; and i run powershell.exe, i get this error :<br/> -------<br/> Windows PowerShell V2 (Community Technology Preview - Features Subject to Change)<br/> Copyright (C) 2008 Microsoft Corporation. All rights reserved.<br/> <br/> AuthorizationManager check failed.<br/> At line:1 char:2<br/> + . &lt;&lt;&lt;&lt; 'C:\Documents and Settings\UserTest\Mes documents\WindowsPowerShell\profile.ps1'<br/>     + CategoryInfo         : NotSpecified: (:) [], PSSecurityException<br/>     + FullyQualifiedErrorId : RuntimeException<br/> <br/> AuthorizationManager check failed.<br/> At line:1 char:2<br/> + . &lt;&lt;&lt;&lt; 'C:\Documents and Settings\UserTest\Mes documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'<br/>     + CategoryInfo         : NotSpecified: (:) [], PSSecurityException<br/>     + FullyQualifiedErrorId : RuntimeExceptionFri, 26 Jun 2009 17:53:09 Z2009-06-26T17:53:09Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#9e355bc2-1819-46d5-b15d-cf349c542952http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#9e355bc2-1819-46d5-b15d-cf349c542952lushdoghttp://social.technet.microsoft.com/Profile/en-US/?user=lushdogDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.Good call, but my service is running so I don't think this is the case for my issue.<br/> <br/> I was thinking that perhaps it is due to the fact that even if you set the execution policy to allow any script to run, you are still prompted to allow a script to run:<br/> <br/> <pre>Security Warning Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your computer. Do you want to run C:\hyperv.ps1? [D] Do not run [R] Run once [S] Suspend [?] Help (default is &quot;D&quot;): r</pre> Perhaps this is why the AuthorizationManager check fail is being raised as there is no user confirmation when dot-sourcing/running a script from .net.<br/>Fri, 26 Jun 2009 18:54:04 Z2009-06-26T18:54:04Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#ca8f8fbd-0e6d-45da-97df-f2b13b5ac732http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#ca8f8fbd-0e6d-45da-97df-f2b13b5ac732Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.If you used IE to download this script, right-click on hyperv.ps1 and select &quot;unblock&quot;.  That might be your problem...<br/>Fri, 26 Jun 2009 19:10:55 Z2009-06-26T19:10:55Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#f5cf3f73-5731-494e-b49e-95f5aae3e301http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32528df8-cb9a-4b2c-b5a8-d39f7fe9df0f#f5cf3f73-5731-494e-b49e-95f5aae3e301lushdoghttp://social.technet.microsoft.com/Profile/en-US/?user=lushdogDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.Yep that got rid of the 'prompt' which made it work ok in .NET. <div><br/></div> <div>Thanks for you help.</div> <div><br/></div> <div>On a side note I went with putting the cmdlet code from the .ps1 file into string resources in my .NET project as I do not want the need for the heightened priviledges required for dot-sourcing a script.</div>Mon, 29 Jun 2009 19:46:31 Z2009-06-29T19:46:31Z