C # problems encountered when calling Power Shell to manage the Office365 Script Execution,
Power Shell management Office Reference http://www.mamicode.com/info-detail-494553.html
C # Call Power Shell reference https://www.cnblogs.com/chenkai/archive/2010/11/09/1872471.html
string pwd = "**********"; string userName = "**********"; StringBuilder ss = new StringBuilder(); ss.AppendLine("$password = ConvertTo-SecureString \"" + pwd + "\" -AsPlainText –Force"); ss.AppendLine("$credential = New-Object System.Management.Automation.PsCredential(\"" + userName + "\",$password)"); ss.AppendLine("$cred = Get-Credential -cred $credential"); ss.AppendLine("Import-Module MSOnline"); //ss.AppendLine("Get-Module"); ss.AppendLine("Connect-Msolservice -cred $cred"); ss.AppendLine("Get-MsolAccountSku"); using (Runspace runspace1 = RunspaceFactory.CreateRunspace()) { Collection<PSObject> results = null; try { runspace1.Open(); Pipeline pipeline = runspace1.CreatePipeline(); pipeline.Commands.AddScript(ss.ToString()); results = pipeline.Invoke(); } catch (Exception ex) { var ss1 = ex.ToString(); } finally { runspace1.Close(); } }
The first error: the "Connect-Msolservice" item cannot be identified as the name of the cmdlet, function, script file, or runable program. Check the spelling of the name. If path is included, make sure the path is correct and try again.
Then it is found that the MSOnline module installed can be successfully imported in 64-bit PowerShell by default, but cannot be imported in 32-bit PowerShell X86,
Error:
Ps c: \ WINDOWS \ system32> Import-Module MSOnline
Import-Module: failed to load the specified Module "MSOnline", because no valid Module File is found in any Module Directory.
Location line: 1 character: 1
+ Import-Module MSOnline
~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: ResourceUnavailable: (MSOnline: String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId: Modules_ModuleNotFound, Microsoft. PowerShell. Commands. ImportModuleCommand
The original module is only installed in 64 bits, and the 32 bits are not installed in the MSOnline module;
Here I refer to the http://just4fun.cn /? P = 786 this article, but some of them are incorrect: for example, "the logon assistant is installed in the 32-bit module library by default, but not in the 64-bit module library ", it is estimated that it has reversed.
C # uses 32-bit PowerShell x86 instead of 64-bit by default;
The following error-prone path is described:
C: \ Windows \ SysWOW64 \ WindowsPowerShell \ v1.0 is a 32-bit Power Shell,
C: \ Windows \ System32 \ WindowsPowerShell \ v1.0 is a 64-bit Power Shell;
Here it is easy to be confused by SysWOW64 and System32, SysWOW64 Folder Program type can refer to Baidu encyclopedia https://baike.baidu.com/item/syswow64/10627204? Fr = aladdin
Later, I found that the 64-bit PowerShell can be successfully called on the Target Platform for modifying the property generated by the project to x64;
Here I will analyze:
If your project is set to a preferred 32-bit dll or any 32-bit dll is referenced, the 32-bit power Shell is automatically called. You can set the target platform to x64 to test whether the 32-bit assembly is dependent on, if the dependency is reported
Failed to Load file or assembly ConnectionOffice365 or one of its dependencies. Attackers try to load programs with incorrect format.
Note:An unhandled exception occurred during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
Exception details:System. BadImageFormatException: failed to Load file, assembly "ConnectionOffice365", or one of its dependencies. Attackers try to load programs with incorrect format.