Powershell 脚本可以在 Powershell 中运行,但不能在 VB.Net 中运行

Pic*_*kle 4 vb.net powershell exchange-server

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

add-pssnapin microsoft.exchange.management.powershell.admin Enable-Mailbox -Identity 'gi joe' -database 'myserver\myserver 邮箱数据库 17'

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

./测试.ps1

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

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

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

Kei*_*ill 5

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

Process.Start("powershell", "-noexit -file c:\<path>\test.ps1")
Run Code Online (Sandbox Code Playgroud)

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

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

var powerShellPath = "C:\Windows\SysNative\WindowsPowerShell\v1.0\powershell.exe"
Process.Start(powerShellPath , "-noexit -file c:\<path>\test.ps1")
Run Code Online (Sandbox Code Playgroud)

抱歉,这可能不是正确的 VB 语法,但应该可以帮助您。