首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理在从c#执行脚本时PowerShell无法找到导入(AzureAd)

如何处理在从c#执行脚本时PowerShell无法找到导入(AzureAd)
EN

Stack Overflow用户
提问于 2019-07-21 18:24:42
回答 1查看 238关注 0票数 2

我正在从c#执行一个PowerShell脚本。脚本使用的是AzureAD模块,但是当我从visual studio执行脚本时,PowerShell告诉我没有找到来自AzureAd模块的所有对象。例如:

代码语言:javascript
复制
"Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail]."

我在PowerShell-ISE中创建了一个脚本来使用azure active-directory进行用户管理,当我在PowerShell中执行该脚本时,它工作得很好,但是当我在visual studio 2017中使用c#执行它时,我提到的错误出现了。

下面是我尝试执行该脚本的一些方法:

代码语言:javascript
复制
    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));

我也试过了:

代码语言:javascript
复制
    PowerShell runspace = PowerShell.Create();
    runspace.AddScript(scriptFullPathName, true).Invoke();

和:

代码语言:javascript
复制
    Runspace runspace =RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    Command command = new Command(scriptFullPathName);
    pipeline.Commands.Add(command);
    retVal = pipeline.Invoke();

脚本中的声明:

代码语言:javascript
复制
    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"

当我在PowerShell中运行脚本时,一切都在工作,我可以毫无问题地进行连接……然而,当我在Visual Studio中执行它时,我得到了以下错误:

代码语言:javascript
复制
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

我还尝试了从Visual Studio执行脚本的其他方法,但无论如何,我总是得到相同的错误...如果有人已经遇到了这个问题,并能给我一个建议,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2019-07-24 15:51:34

首先,在visual studio ->中,右键单击您的项目->,选择Properties -> Build,取消选中platform目标下的bit 32-bit

然后运行该项目,看看它是否正常工作。

我使用下面的代码做了一个简单的测试,它对我来说是有效的(你可以修改代码来满足你的需求):

代码语言:javascript
复制
        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();
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57132264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档