繁体   English   中英

Powershell脚本适用于Powershell,但不适用于VB.Net

[英]Powershell script works in Powershell, but not in VB.Net

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

add-pssnapin microsoft.exchange.management.powershell.admin Enable-Mailbox -Identity'gi joe'-database'myserver \\ myserver mailbox database 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(snapin或模块可能只在那里可用)。 在这种情况下,您需要按路径调用PowerShell,并且必须在路径中使用SysNative才能看到64位PowerShell目录。

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

对不起,这可能不是正确的VB语法,但应该让你去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM