Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell脚本在Powershell中工作,但在VB.Net中不工作_Vb.net_Powershell_Exchange Server - Fatal编程技术网

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

Powershell脚本在Powershell中工作,但在VB.Net中不工作,vb.net,powershell,exchange-server,Vb.net,Powershell,Exchange Server,我有一个简单的powershell脚本来启用Exchange中名为test.ps1的邮箱。以下是脚本: 在microsoft.exchange.management.powershell.admin中添加pssnapin 启用邮箱-标识“gi joe”-数据库“myserver\myserver邮箱数据库17” 如果我转到Powershell控制台并键入 /test.ps1 它将成功运行。但是,如果我在VB.net中使用 进程启动(“powershell”、“test.ps1”) 终端闪烁得太快

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

在microsoft.exchange.management.powershell.admin中添加pssnapin 启用邮箱-标识“gi joe”-数据库“myserver\myserver邮箱数据库17”

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

/test.ps1

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

进程启动(“powershell”、“test.ps1”)


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

若要查看出了什么问题,请尝试以下方法:

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

很抱歉,这可能不是正确的VB语法,但应该可以让您继续使用。

谢谢,原来错误是“没有为Windows PowerShell版本2注册管理单元”。这很奇怪,因为脚本的第一行添加了管理单元。有没有一种方法可以替换“powershell”以将powershell包含在所有moduel中?这样我就不需要那行代码来添加管理单元了。@Austin这听起来像是你遇到了32位vs 64位的问题。在32位shell中注册管理单元时,它不会自动为64位shell注册,反之亦然。你的VB应用程序是64位运行还是32位运行?你说得对,我将目标cpu更改为任何cpu,它工作了!然而,这破坏了我的应用程序的另一部分=(是否有办法强制Powershell以32位模式启动?是的,请使用Powershell 32位版本的路径,例如
C:\Windows\SysWow64\WindowsPowershell\v1.0\Powershell.exe
。当然,您可能希望使用
System.Environment
类来获取
windir
环境变量,而不是硬代码
C。):\windows
.Hmm我仍然会遇到同样的错误。你知道为什么我将它设置为任何CPU时它都能工作,而设置为x86时却不能工作吗?
var powerShellPath = "C:\Windows\SysNative\WindowsPowerShell\v1.0\powershell.exe"
Process.Start(powerShellPath , "-noexit -file c:\<path>\test.ps1")