Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script works in Powershell, but not in VB.Net

I have a simple powershell script to enable a mailbox in Exchange called test.ps1. Here is the script:

add-pssnapin microsoft.exchange.management.powershell.admin Enable-Mailbox -Identity 'gi joe' -database 'myserver\myserver mailbox database 17'

If I go to the Powershell console and type

./test.ps1

It will run successfully. However, if I call it in VB.net using

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

the terminal flashes too quickly for me to see what it says, and it doesn't create the mailbox. Why does this happen, or how can I stop the terminal from disappearing before I can read the error?

like image 577
Pickle Avatar asked Sep 12 '25 09:09

Pickle


1 Answers

To see what is going wrong try this instead:

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

I suspect the error you get is because you aren't providing a full path to test.ps1.

Another possibility is that your 32-bit VB app needs to launch 64-bit Powershell (snapin or module may only be available there). In that case you need to invoke PowerShell by path and you have to use SysNative in your path to be able to see the 64-bit PowerShell dir.

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

Sorry that's probably not proper VB syntax but should get you going.

like image 96
Keith Hill Avatar answered Sep 13 '25 23:09

Keith Hill