无法在ASP.NET中导入模块MSOnline
我们创建了一个用于在asp.net中运行PowerShell的迷你Web程序。 PowerShell命令运行良好。但是,当我们试图导入的MSOnline模块,它会显示以下错误:无法在ASP.NET中导入模块MSOnline
Could not load file or assembly 'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Dim Ps As PowerShell = PowerShell.Create()
Dim tempScript As String = "Import-Module MSOnline " + Environment.NewLine
tempScript = tempScript + "$username = '[email protected]' " + Environment.NewLine
tempScript = tempScript + "$password = 'abcd1234' " + Environment.NewLine
tempScript = tempScript + "$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force " + Environment.NewLine
tempScript = tempScript + "$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd " + Environment.NewLine
tempScript = tempScript + "Connect-MsolService -Credential $creds " + Environment.NewLine
tempScript = tempScript + "Get-MsolDomain" + Environment.NewLine
Ps.Commands.AddScript(tempScript)
' Execute the script
Dim results = Ps.Invoke()
PS C:> $ PSVersionTable |输出字符串
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
更新:3月 - 2017年
尝试使用的Process.Start调用CMD,然后调用CMD PowerShell的,但它也失败了。相同的错误信息。
Private Function RunBatch() As String
Dim proc As New Process
Dim strBuilder As StringBuilder = New System.Text.StringBuilder
proc.StartInfo.FileName = "C:\temp\enableMFA.bat"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
proc.WaitForExit()
Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
For Each ln As String In output
strBuilder.Append(ln & vbNewLine + vbLf)
Next
Return strBuilder.ToString
End Function
错误消息:
C:\Users\admin\Desktop\Preview\Preview\bin\Debug>powershell -File "C:\temp\enableMFA.ps1"
Import-Module : Could not load file or assembly 'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll' or
one of its dependencies. An attempt was made to load a program with an incorrect format.
At C:\temp\enableMFA.ps1:1 char:1
Import-Module MSOnline
~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [Import-Module], BadImageFormatException
FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
通过改变最后解决了该问题的CPU到x64,这是步骤
Right click the project >> properties >> complie >> Target CPU to x64
Go to TOOLS >> Options >> Projects and Solutions >> Web Projects >> use 64bit IIS Express
**记住关闭VS2013,然后再次打开该项目**
** Thx for the hints from David Brabant**
很可能是一点点问题M& https://ziaahmedshaikh.wordpress.com/2015/09/11/configure-windows-powershell-to-connect-with-o356/ –
我尝试使用VB.NET中的process.start调用PowerShell,也失败 – user1172579