C# 如何处理PowerShell在从c执行ascript时无法找到导入(AzureAd)的问题#
我正在从c#执行PowerShell脚本。脚本正在使用AzureAD模块,但当我从visual studio PowerShell执行脚本时,会显示未找到来自C# 如何处理PowerShell在从c执行ascript时无法找到导入(AzureAd)的问题#,c#,powershell,azure-active-directory,C#,Powershell,Azure Active Directory,我正在从c#执行PowerShell脚本。脚本正在使用AzureAD模块,但当我从visual studio PowerShell执行脚本时,会显示未找到来自AzureAD模块的所有对象。 例如: "Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail]." 我在PowerShell ISE中创建了一个脚本,以使用azure active directory进行用户管理,当我在PowerShell中执行脚本时,它工作正常,
AzureAD
模块的所有对象。
例如:
"Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail]."
我在PowerShell ISE中创建了一个脚本,以使用azure active directory进行用户管理,当我在PowerShell中执行脚本时,它工作正常,但是当我在visual studio 2017中使用c#执行脚本时
我所说的错误出现了
以下是我尝试执行脚本的一些方法:
字符串文件名=@“CreateOneDriveApp.ps1”;
var scriptFullPathName=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“文件”,文件名);
ProcessStartInfo startInfo=新的ProcessStartInfo();
startInfo.FileName=@“powershell.exe”;
Arguments=scriptFullPathName;
startInfo.RedirectStandardOutput=true;
startInfo.RedirectStandardError=true;
startInfo.UseShellExecute=false;
startInfo.CreateNoWindow=false;
流程=新流程();
process.StartInfo=StartInfo;
process.Start();
字符串错误=process.StandardError.ReadToEnd();
IsTrue(string.IsNullOrEmpty(errors));
我还尝试:
PowerShell运行空间=PowerShell.Create();
AddScript(scriptFullPathName,true).Invoke();
以及:
Runspace Runspace=RunspaceFactory.CreateRunspace(runspaceConfiguration);
Open();
Pipeline Pipeline=runspace.CreatePipeline();
Command=新命令(scriptFullPathName);
pipeline.Commands.Add(命令);
retVal=pipeline.Invoke();
脚本中的声明:
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执行它时,会出现以下错误:
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
我还尝试了从VisualStudio执行脚本的其他方法,但无论发生什么,我总是会遇到相同的错误。。。如果有人已经遇到这个问题并能给我一个建议,我将不胜感激。首先,在visual studio中->右键单击您的项目->选择属性->构建,取消选中平台目标下的首选32位: 然后运行项目,看看它是否有效 我使用下面的代码做了一个简单的测试,它对我很有用(您可以修改代码以满足您的需要):
你能用下面的答案解决这个问题吗?
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();
}