Использование командлетов Quest Active Directory для Powershell путем вызова сценария PS - PullRequest
0 голосов
/ 08 мая 2018

Код C # ( Источник ):

private string RunScript(string scriptText)
{
    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it

    runspace.Open();

    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.

    pipeline.Commands.Add("Out-String");

    // execute the script

    Collection<psobject /> results = pipeline.Invoke();

    // close the runspace

    runspace.Close();

    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

Код Powershell

#Dummy code for example purpose

ASNP Quest*
#Example of cmdlet I want to use
$Users = Get-QADGroupMember -Identity $Group -Enabled
return $Users.count

Как видите, моя цель - вызвать скрипт, использующий RunScript выше, в Button_Click event в моем приложении WPF. Мне удалось правильно вызвать скрипт, но вызов командлетов Quest явно не проходит так, как хотелось бы, так как я получил бы 0 в приведенном выше примере.

TL; DR

Скрипт работает правильно, но вызовы командлетов Quest не работают, поскольку он ничего не возвращает (или 0 в приведенном выше примере). Я что-то упускаю?

EDIT

Важно отметить, что точно такой же скрипт, запущенный в Powershell, возвращает правильные значения. Вызывать это из C # не надо.

...