簡體   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