从c#运行MFA powershell脚本

从c#运行MFA powershell脚本

问题描述:

我有一个在powershell中执行的power shell wcript,但是没有从c#执行的相同代码。

I have a power shell wcript which is executing in powershell, but the same code not executing from 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);

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

我收到此错误。

Am getting this error.


  1. 术语"Connect-MsolService"无法识别为cmdlet,函数,脚本文件或可操作程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
  2. 找不到类型[Microsoft.Online.Administration.StrongAuthenticationRequirement]:验证包含此内容的程序集类型已加载。 

Hello sandy1985,

Hello sandy1985,

尝试以下代码。

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

//调用管道上的执行(收集输出)
var PSOutput = PowerShellInstance.Invoke();

//遍历每个输出对象项
foreach(PSO输出中的PSObject outputItem)
{
if(outputItem!= null)
{
Console.WriteLine(outputItem.ToString());
}
}
}
}
Console.ReadLine();
}

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(); }

注意。当您在C#中执行多个命令时,您需要使用";"。分开。请注意脚本工作目录。

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

祝你好运,

Neil Hu