Powershell脚本在Powershell中工作,但在 VB.Net 中不起作用



我有一个简单的Powershell脚本来启用Exchange中一个名为test.ps1的邮箱。 这是脚本:

add-psSnapin Microsoft.Exchange.Management.PowerShell.admin启用邮箱 -标识 'gi joe' -database 'myserver\myserver 邮箱数据库 17'

如果我转到Powershell控制台并键入

.

/test.ps1

它将成功运行。 但是,如果我在 VB.net 使用

Process.Start("powershell", "test.ps1")

终端闪烁得太快,我看不到它说了什么,并且它不会创建邮箱。 为什么会发生这种情况,或者如何在读取错误之前阻止终端消失?

要查看出了什么问题,请尝试以下操作:

Process.Start("powershell", "-noexit -file c:<path>test.ps1")

我怀疑您收到的错误是因为您没有提供 test.ps1 的完整路径。

另一种可能性是 32 位 VB 应用需要启动 64 位 Powershell(管理单元或模块可能仅在那里可用)。 在这种情况下,您需要按路径调用PowerShell,并且必须在路径中使用SysNative才能看到64位PowerShell目录。

var powerShellPath = "C:WindowsSysNativeWindowsPowerShellv1.0powershell.exe"
Process.Start(powerShellPath , "-noexit -file c:<path>test.ps1")

抱歉,这可能不是正确的 VB 语法,但应该让您继续前进。

最新更新