IT プロフェッショナルのための技術情報サイト > フォーラム ホーム > Windows PowerShell > Dot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.
質問する質問する
 

回答済みDot-Sourcing a PowerShell script in .NET and then running a cmdlet defined in the script.

  • 2009年6月25日 18:04lushdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    The HyperV guys have a nice hyperv.ps1 script that contains definitions for cmdlets like (Get-VM, Get-VMHost etc. etc.).

    So in the PowerShell command line i dot source (. ./hyperv.ps1) then run 'Get-VM' and all is well.

    I'd like to do the same from .Net:

    string scriptContents = string.Empty;
    using(StreamReader fs = new StreamReader(@"hyperv.ps1"))
    {
      scriptContents = fs.ReadToEnd();
    }
    .
    .
    .
    .
    pipeline.Commands.AddScript("scriptContents");
    pipeline.Command.Add(new Command("Get-VM"));
    pipeline.Invoke();
    
    
    Unfortunately I get a CommandNOtFoundException :"The term 'Get-VMHost' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again." 

    Thanks for your help in advance.

    Matt




回答

すべての返信

  • 2009年6月25日 18:31Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    That's a good one.

    Since this is a runspace, I think I'd try this first by modifying the function in hyper.ps1:

    Instead of:
    function get-vm {...
    }

    Change it to:
    function global:get-vm {...
    }

    Then when you load hyper.ps1, I think you will have access to the function in your current scope.

    I don't know what might be the implications of changing *all* the functions to be in the global scope in this case.
  • 2009年6月25日 22:42lushdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    I figured out that i should be loading the script in the runspace rather than the pipeline:

    myRunspaceInvoke.Invoke(". ./hyperv.ps1");
    
    

    Unfortunately I get a "AuthorizationManager check failed." exception as I believe the script is not signed or some other authentication problem.
  • 2009年6月26日 13:38Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Are you using the new v2 CTP3 here?

    Known problem:
    https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=466285&SiteID=99
  • 2009年6月26日 17:17lushdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Nope I'm using Powershell 1.0.

    I can't seem to find that feedback item you've linked.

    Can you cut and paste the summary here so I can read it over?
  • 2009年6月26日 17:53Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答の候補
    I'll have to try some more testing with runspaces.  This may be related directly to WMI though...

    When the WMI service (winmgmt) is "paused", the policy execution equal to "AllSigned" and i run powershell.exe, i get this error :
    -------
    Windows PowerShell V2 (Community Technology Preview - Features Subject to Change)
    Copyright (C) 2008 Microsoft Corporation. All rights reserved.

    AuthorizationManager check failed.
    At line:1 char:2
    + . <<<< 'C:\Documents and Settings\UserTest\Mes documents\WindowsPowerShell\profile.ps1'
        + CategoryInfo         : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException

    AuthorizationManager check failed.
    At line:1 char:2
    + . <<<< 'C:\Documents and Settings\UserTest\Mes documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
        + CategoryInfo         : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException
  • 2009年6月26日 18:54lushdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    Good call, but my service is running so I don't think this is the case for my issue.

    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:

    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 "D"): r
    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.
  • 2009年6月26日 19:10Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    If you used IE to download this script, right-click on hyperv.ps1 and select "unblock".  That might be your problem...
  • 2009年6月29日 19:46lushdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Yep that got rid of the 'prompt' which made it work ok in .NET.

    Thanks for you help.

    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.