0

C# code (Source):

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 code

#Dummy code for example purpose

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

As you can see, my goal is to call the script utilizing RunScript above in a Button_Click event in my WPF app. I've been able to correctly call the script but the call to Quest cmdlets clearly doesn't go trough as wanted since I would receive 0 in the above example.

TL;DR

Script is running correctly but calls to Quest cmdlets don't work since it return nothing (or 0 in the above example). Is there something I'm missing ?

EDIT

Important to note that the exact same script ran in Powershell returns the correct values. Calling it from C# don't.

scharette
  • 9,437
  • 8
  • 33
  • 67
  • @TheIncorrigible1 Within c# you mean ? – scharette May 08 '18 at 14:09
  • Is there a reason your using c# at all? Powershell has support for WPF front-ends – Mike Garuccio May 08 '18 at 14:14
  • 1
    @MikeGaruccio Not very well. It's significantly more effort if you're doing anything slightly advanced. – Maximilian Burszley May 08 '18 at 14:15
  • I'm not sure the `Out-String` command is doing what you hope for. You seem to be confusing executing a full script and a separate command. Can you get _anything_ to return? For example, a simple string or output from, say, `Get-Process`? This will rule out the Quest cmdlets. – boxdog May 08 '18 at 14:21
  • @boxdog good call. Just tried, and `Get-Proscess` is returned exactly as I would expect. As i said, I get return values in the good form, but calling Quest cmdlets don't return correct values. BTW, calling directly from PS returns correct values. Added edit accordingly. – scharette May 08 '18 at 14:28
  • @TheIncorrigible1 I'm trying to import it but I"m not sure i"m doing it correctly. – scharette May 08 '18 at 14:38
  • @TheIncorrigible1 Already tried, without success _The term 'Import-Module -Name QuestAD' is not recognized as the name of a cmdlet, function, script file, or operable program._ – scharette May 08 '18 at 14:51
  • Are you using `Import-Module` in your script? I'd suggest adding that command there. Is the module named QuestAD? I was guessing. – Maximilian Burszley May 08 '18 at 14:55
  • If it's a possibility, try using the Microsoft `ActiveDirectory` module. I tried your C# code with that and it works fine (using `Get-ADGroupMember`). I don't have the Quest cmdlets to test with. Incidentally, where is `$group` defined? I just substituted a hard-coded name for my test, but it isn't clear where you are defining it. – boxdog May 08 '18 at 15:03
  • @TheIncorrigible1 the correct command seems to be `Add-PSSnapin Quest.ActiveRoles.ADManagement` but adding it to the top of my script didn't work... – scharette May 08 '18 at 15:03
  • Add this to the head of your script: `#Requires -PSSnapin Quest.ActiveRoles.ADManagement` – Maximilian Burszley May 08 '18 at 15:07
  • @TheIncorrigible1 _the following snap-ins that are specified by the "#requires" statements of the script are missing: Quest.ActiveRoles.ADManagement.'_ So the problem comes from there... I'll look into it. Thanks ! – scharette May 08 '18 at 15:12
  • @TheIncorrigible1 @boxdog @Mike Garuccio Found the problem... Was a real pain to debug. It had nothing to do with importing... I was calling a `Get-Content` on a file which wasn't correctly resolved, i was therefore comparing against null values. I'm truly thankful for your time and sorry my bug was directly related to the question. Should I remove the post ? – scharette May 08 '18 at 15:43
  • @scharette Usually typos / simple mistakes are closed so I'd say yes – Maximilian Burszley May 08 '18 at 15:44
  • I'ts not really a typo but if others agree I will. – scharette May 08 '18 at 15:44

0 Answers0