gpt4 book ai didi

asp.net - 在 ASP.NET 中导入模块 MSOnline 失败

转载 作者:行者123 更新时间:2023-12-03 00:46:03 26 4
gpt4 key购买 nike

我们创建了一个用于在 asp.net 中运行 PowerShell 的迷你 Web 程序。 PowerShell 命令运行良好。但是当我们尝试导入 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 = 'xxx@xxx.com' " + 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

更新:2017 年 8 月 3 日

尝试使用 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**

关于asp.net - 在 ASP.NET 中导入模块 MSOnline 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com