TechNet - Только для профессионалов. Специально для Вас. > Форумы > 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.

  • 25 июня 2009 г. 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




Ответы

Все ответы