从C#调用Azure AD预览导致RestSharp MissingMethodException,在ISE中正常工作

时间:2017-06-28 13:12:51

标签: c# powershell azure azure-active-directory restsharp

我收到错误""找不到方法:' RestSharp.IRestRequest RestSharp.RestRequest.AddQueryParameter(System.String,System.String)'。 "使用C#中的AzureADPreview cmdlet和运行空间调用时。该脚本在PowerShell ISE中完美运行。

这是脚本:

Import-Module MSOnline
Import-Module AzureADPreview

Write-Host Creating credentials...
$adminUsername = "removed"
$adminPassword = "removed"
$securePassword = ConvertTo-SecureString -String $adminPassword -AsPlainText -Force

# Attempt to get credentials object
$credentials = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername, $securePassword

Connect-AzureAD -Credential $credentials

# Delete the non-enterprise apps.
Write-Host Deleting non-enterprise apps...
Get-AzureADApplication -ErrorAction Stop | Select ObjectId | ForEach-Object { Remove-AzureADApplication -ObjectId $_.ObjectId }

Return "Run completed without error."

这与PowerShell ISE完美配合。

但是,C#会在Get-AzureADApplication行中抛出上述错误。有什么想法吗?

public static string GetScriptResultString(string scriptText)
{
    // Create Powershell runspace with access to MS Online.
    var initialState = InitialSessionState.CreateDefault();
    initialState.ImportPSModule(new[] { "MSOnline" }); // ensure we have access to Msol-Connect and so forth.
    initialState.UseFullLanguageModeInDebugger = true;
    initialState.LanguageMode = PSLanguageMode.FullLanguage;
    using (var runspace = RunspaceFactory.CreateRunspace(initialState))
    {
        // Open the runspace.
        runspace.Open();

        // Invoke as necessary.
        var invoker = new RunspaceInvoke(runspace);
        invoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force");

        // Create a pipeline and feed it the script text.
        try
        {
            using (var pipeline = runspace.CreatePipeline())
            {
                pipeline.Commands.AddScript(scriptText);

                // Return the script outputs as strings.
                pipeline.Commands.Add("Out-String");

                // execute the script
                var results = pipeline.Invoke();

                // close the runspace
                runspace.Close();

                // Convert the script result into a single string
                var stringBuilder = new StringBuilder();
                foreach (var obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }

                return stringBuilder.ToString();
            }

        }
        catch (Exception ex)
        {
            var msg = ex.GetBaseException().Message;
            Debug.WriteLine($"GetScriptResultString(): Exception occurred: { msg }");
            return null;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

要解决此问题,我添加了对https://www.nuget.org/packages/RestSharp/的引用,然后在PowerShell脚本中添加了:

Add-Type -Path RestSharp.dll

我不知道为什么在PowerShell ISE和PowerShell直接使用此更改时无效。更新后的脚本如下所示:

Import-Module MSOnline
Import-Module AzureADPreview

Add-Type -Path RestSharp.dll

Write-Host Creating credentials...
$adminUsername = "@@USERNAME@@"
$adminPassword = "@@PASSWORD@@"
$securePassword = ConvertTo-SecureString -String $adminPassword -AsPlainText -Force

# Attempt to get credentials object
$credentials = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername, $securePassword

Connect-AzureAD -Credential $credentials

# Note the list apps may not update immediately. Wait a minute or so before checking the app lists.

# Delete the non-enterprise apps.
Write-Host Deleting non-enterprise apps...
Get-AzureADApplication -ErrorAction Stop | Select ObjectId | ForEach-Object { Remove-AzureADApplication -ObjectId $_.ObjectId }

Return "Run completed without error."

答案 1 :(得分:0)

我也遇到了同样的错误。卸载 RestSharp.dll 并从 nuget 重新安装,然后关闭和打开 Visual Studio2015 解决了我的问题。