Jonathan
Jonathan

Reputation: 21

How to deal with PowerShell not being able to find an import (AzureAd) while executing ascript from c#

I'm executing a PowerShell script from c#. the script is using the AzureAD module but when I execute the script from visual studio PowerShell is saying that all the object from the AzureAd module are not found. For example:

"Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail]."

I created a script in PowerShell-ISE to use the azure active-directory for the purpose of user management and when I execute the script inside PowerShell it works just fine, however when I execute it using c# in visual studio 2017 the errors I talked about appears.

Here are some of the ways I tried to execute the script:


    string fileName = @"CreateOneDriveApp.ps1";
    var scriptFullPathName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files", fileName);
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"powershell.exe";
    startInfo.Arguments = scriptFullPathName;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = false;
    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();
    string errors = process.StandardError.ReadToEnd();
    Assert.IsTrue(string.IsNullOrEmpty(errors));

I also tried :


    PowerShell runspace = PowerShell.Create();
    runspace.AddScript(scriptFullPathName, true).Invoke();

and :


    Runspace runspace =RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    Command command = new Command(scriptFullPathName);
    pipeline.Commands.Add(command);
    retVal = pipeline.Invoke();

the declarations in the scripts:


    param([PSCredential]$Credential="", [string]$tenantId="f83db3f0-e369-449d-a871-a98d916f9dd9")
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
    Install-Module AzureAD
    Import-Module AzureAD
    Add-Type -Path Microsoft.Azure.ActiveDirectory.GraphClient.dll"

When I'm running the script in PowerShell everything is working and I can connect without any problems... However when I'm executing it from Visual Studio I'm getting the following errors:

At CreateOneDriveApp.ps1:76 char:6
+     [Microsoft.Open.AzureAD.Model.TenantDetail] $TenantDetails; #for  ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail].
At CreateOneDriveApp.ps1:149 char:10
+         [Microsoft.Open.AzureAD.Model.RequiredResourceAccess] $requir ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.Open.AzureAD.Model.RequiredResourceAccess].
At CreateOneDriveApp.ps1:150 char:10
+         [Microsoft.Open.AzureAD.Model.PasswordCredential] $passwordCr ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.Open.AzureAD.Model.PasswordCredential].
At CreateOneDriveApp.ps1:332 char:6
+     [Microsoft.Open.AzureAD.Model.PasswordCredential]GeneratePassword ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.Open.AzureAD.Model.PasswordCredential].
At CreateOneDriveApp.ps1:351 char:6
+     [Microsoft.Open.AzureAD.Model.RequiredResourceAccess]GenerateRequ ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.Open.AzureAD.Model.RequiredResourceAccess].
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TypeNotFound

I also tried other ways of executing the script from Visual Studio but I always got the same error no matter what... and I will very appreciate if someone already encountered this problem and can give me an advice.

Upvotes: 2

Views: 372

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

First, in visual studio -> right click your project -> select Properties -> Build, uncheck the Prefer 32-bit under the platform target:

enter image description here

Then run the project, to see if it works.

I did a simple test use the code below, and it works for me(you can modify the code to meet your need):

        static void Main(string[] args)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();

            pipeline.Commands.AddScript("Import-Module AzureAD -Force;");
            pipeline.Commands.AddScript("New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess");
            var result = pipeline.Invoke();


            Console.WriteLine("done");
            Console.ReadLine();
        }

Upvotes: 1

Related Questions