2017-08-03 117 views
1

我們創建了一個用於在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

+1

很可能是一點點問題M& https://ziaahmedshaikh.wordpress.com/2015/09/11/configure-windows-powershell-to-connect-with-o356/ –

+0

我嘗試使用VB.NET中的process.start調用PowerShell,也失敗 – user1172579

回答

2

通過改變最後解決了該問題的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**