Vb.net 从WinForms应用程序中导入模块不工作
我有一个VB.net应用程序,我从VB.net窗口应用程序中调用PowerShell上的Vb.net 从WinForms应用程序中导入模块不工作,vb.net,powershell,Vb.net,Powershell,我有一个VB.net应用程序,我从VB.net窗口应用程序中调用PowerShell上的导入模块,但错误显示找不到模块。错误如下 Import-Module Msonline Connect-msolService 导入模块:未加载指定的模块“MSOnline”,因为在任何模块目录中都找不到有效的模块文件 当我以通常的方式从外部启动PowerShell来加载相同的模块时,它工作正常。如下图所示 Import-Module Msonline Connect-msolService VB脚本
导入模块
,但错误显示找不到模块。错误如下
Import-Module Msonline
Connect-msolService
导入模块:未加载指定的模块“MSOnline”,因为在任何模块目录中都找不到有效的模块文件
当我以通常的方式从外部启动PowerShell来加载相同的模块时,它工作正常。如下图所示
Import-Module Msonline
Connect-msolService
VB脚本如下所示
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim procStartInfo As New ProcessStartInfo
Dim procExecuting As New Process
With procStartInfo
.UseShellExecute = True
.FileName = "powershell.exe"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
Dim psscript As String = My.Resources.mymsolPS
procStartInfo.Arguments = psscript
procExecuting = Process.Start(procStartInfo)
End With
端接头
我的PowerShell脚本作为txt文件保存在My.resource中。我的PowerShell脚本如下所示
Import-Module Msonline
Connect-msolService
我将PowerShell脚本替换为获取帮助
,只有当我使用导入模块
Msonline时,它才起作用
可以共享的另一个信息是模块存储在以下位置
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\MSOnline.psd1
非常感谢您的帮助。
提前谢谢
更新2:
我不确定这是否有关系
如果我从VB.net中启动powershell并运行以下命令,我将看不到MSOnline模块
PS C:\windows\system32>> cd $env:WINDIR\System32\WindowsPowerShell\v1.0\Modules\
PS C:\windows\System32\WindowsPowerShell\v1.0\Modules>> dir
如果我直接从系统运行PowerShell并运行上述脚本,我可以看到模块
d----- 11/22/2017 2:59 PM MSOnline
d-----2017年11月22日下午2:59 MSOnline
对我来说仍然是个谜,我无法解开(我注意到的一个区别是,当从应用程序启动时,或者本地启动时,目录是您的用户或系统。因此,可能PS的加载方式无法找到模块 如果提供模块的完整路径,该怎么办 我使用运行空间的运气要好得多-我使用它来传递任何powershell命令-以下是其中一个站点的片段和一些示例:
'Create the runspace.
Using R As System.Management.Automation.Runspaces.Runspace = _
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
'Create the pipeline
Using P As System.Management.Automation.Runspaces.Pipeline = R.CreatePipeline()
'Open the runspace.
R.Open()
'Create each command (in this case just one)...
Dim Cmd As New System.Management.Automation.Runspaces.Command("C:\script.ps1", True)
'...and add it to the pipeline.
P.Commands.Add(Cmd)
'Execute the commands and get the response.
Dim Result As System.Collections.ObjectModel.Collection(Of _
System.Management.Automation.PSObject) = P.Invoke()
'Close the runspace.
R.Close()
'Display the result in the console window.
For Each O As System.Management.Automation.PSObject In Result
Console.WriteLine(O.ToString())
Next
End Using
End Using
最后一个提供了它正在做的事情的一个非常可靠的分解。如果你不能让它工作,请告诉我,我可以尝试在应用程序上查看此导入是否有效。经过几个小时的痛苦,我实际上找到了解决方案。这是一个非常愚蠢的解决方案 转到我的应用程序属性,我发现首选运行被设置为32位,因此当我的PowerShell从内部启动时,它正在SYSWOW下查找模块,而它应该在System32下查找。我取消选中“首选32位”,而不是从System32导入模块
我想我应该分享这个愚蠢的失误,这样其他人就不会遭受同样的损失。当我使用我常用的PowerShell脚本时,上面的命令非常有用。例如,如果我用get help替换脚本文件,但是如果我使用import Module,它就找不到该模块。我隔离的是如果我键入“CD C:\Windows\System32\WindowsPowerShell\v1.0\Module”es\MSOnline\'我可以访问该位置,但不能从应用程序中启动的PowerShell访问