我正在从c#执行一个PowerShell脚本。脚本使用的是AzureAD模块,但是当我从visual studio执行脚本时,PowerShell告诉我没有找到来自AzureAd模块的所有对象。例如:
"Unable to find type [Microsoft.Open.AzureAD.Model.TenantDetail]."我在PowerShell-ISE中创建了一个脚本来使用azure active-directory进行用户管理,当我在PowerShell中执行该脚本时,它工作得很好,但是当我在visual studio 2017中使用c#执行它时,我提到的错误出现了。
下面是我尝试执行该脚本的一些方法:
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));我也试过了:
PowerShell runspace = PowerShell.Create();
runspace.AddScript(scriptFullPathName, true).Invoke();和:
Runspace runspace =RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command(scriptFullPathName);
pipeline.Commands.Add(command);
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我还尝试了从Visual Studio执行脚本的其他方法,但无论如何,我总是得到相同的错误...如果有人已经遇到了这个问题,并能给我一个建议,我将非常感激。
发布于 2019-07-24 15:51:34
首先,在visual studio ->中,右键单击您的项目->,选择Properties -> Build,取消选中platform目标下的bit 32-bit:

然后运行该项目,看看它是否正常工作。
我使用下面的代码做了一个简单的测试,它对我来说是有效的(你可以修改代码来满足你的需求):
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();
}https://stackoverflow.com/questions/57132264
复制相似问题