从c#运行MFA powershell脚本

时间:2018-01-30 08:29:19

标签: c# azure-powershell

我有一个在PowerShell中执行的power shell脚本,但是相同的代码没有从C#执行。这是我的代码。

PowerShellScript psScript = new PowerShellScript();
            //psScript.AddLine("Import-Module msonline");
            psScript.AddLine("Set-ExecutionPolicy RemoteSigned -Scope CurrentUser");
            psScript.AddLine("$ServiceUser = \"" + this.ServiceUser + "\"");
            psScript.AddLine("$ServicePassword = ConvertTo-SecureString -AsPlainText \"" + this.ServicePassword + "\" -Force");
            psScript.AddLine("$Livecred = New-Object System.Management.Automation.PSCredential -ArgumentList $ServiceUser, $ServicePassword");
            psScript.AddLine("Connect-MsolService -Credential $Livecred");
            psScript.AddLine("$upn = \"" + username + "\"");
            psScript.AddLine("$auth = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement");
            psScript.AddLine("$auth.RelyingParty = '*'");
            psScript.AddLine("$auth.State = 'Enforced'");
            psScript.AddLine("$auth.RememberDevicesNotIssuedBefore = (Get-Date)");
           // psScript.AddLine("Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationRequirements $auth");
            psScript.AddLine("$mfaoff = @()");
            psScript.AddLine("Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationRequirements $mfaoff");


            Console.WriteLine("SCRIPT:");
            Console.WriteLine(psScript.GetScript());

            pshell.AddScript(psScript.GetScript());
            pshell.AddCommand("Out-String");

            PowerShellResponse response = new PowerShellResponse();
            response.InvokeResponse(pshell);
            msox.ResponseList.AddRange(response.PropertyList);
            msox.Success = (response.ErrorList.Count == 0);

我收到此错误。

  

1.“Connect-MsolService”一词不被视为a的名称       cmdlet,函数,脚本文件或可操作程序。检查       拼写名称,或者如果包含路径,请验证       路径是正确的,然后再试一次。找不到类型

     

2 Microsoft.Online.Administration.StrongAuthenticationRequirement]:       验证是否已加载包含此类型的程序集。

0 个答案:

没有答案