Run MFA powershell script from c# RRS feed

  • Question

  • I have a power shell wcript which is executing in powershell, but the same code not executing from c#.

    here is my code.

    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");



                    PowerShellResponse response = new PowerShellResponse();
                    msox.Success = (response.ErrorList.Count == 0);

    Am getting this error.

    1. The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    2. Cannot find type [Microsoft.Online.Administration.StrongAuthenticationRequirement]: verify that the assembly containing this type is loaded. 

    Tuesday, January 30, 2018 7:12 AM

All replies

  • Hello sandy1985,

    Try the below code.

    static void Main(string[] args) { //change powershell work directory using (Runspace runspace = RunspaceFactory.CreateRunspace()) { Environment.CurrentDirectory = "D:\\Test7"; runspace.Open(); using (PowerShell PowerShellInstance = PowerShell.Create()) { // I can't test your script so I use the multi simple command instead.

    PowerShellInstance.AddScript("Get-ChildItem;ipconfig"); // invoke execution on the pipeline (collecting output) var PSOutput = PowerShellInstance.Invoke(); // loop through each output object item foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { Console.WriteLine(outputItem.ToString()); } } } } Console.ReadLine(); }

    Note. When you execute multi commands in C# you need to use ";" to split. And please note the script working directory.

    Best regards,

    Neil Hu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, January 31, 2018 8:12 AM