Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to launch process that won't close? 1

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I am working on a utility that builds a PowerShell PS1 file. I have my VB.Net application launching that PS1 file as follows:

Code:
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Dim TempPath As String
        TempPath = My.Computer.FileSystem.SpecialDirectories.Temp
        Dim FILE_NAME As String = TempPath & "\InstallPreReqs.ps1"
        OpenPowerShell(FILE_NAME)
End Sub

Private Sub OpenPowerShell(ByVal f As String)
        Dim startInfo As New ProcessStartInfo
        startInfo.FileName = "PowerShell.EXE"
        startInfo.Arguments = f
        Process.Start(startInfo)
End Sub

Clicking my button is successfully launching my PowerShell process, but at the end of the script the process terminates and I can't read the screen input. Can anyone tell me a means to prevent the process from terminating?

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Unfortunately it's not intuitive.

Firstly run PowerShell
As soon as it opens press Enter
Then type [tt]Get-ExecutionPolicy[/tt] and press Enter
It will probably return [tt]Restricted[/tt]
Then either type
[tab][tt]Set-ExecutionPolicy Unrestricted[/tt] and press Enter
Or type
[tab][tt]Set-ExecutionPolicy RemoteSigned[/tt] and press Enter
Or type
[tab][tt]Set-ExecutionPolicy AllSigned[/tt] and press Enter

Which you choose will depend on how much access you want to give scripts -
[tab]Unrestricted - self explanatory
[tab]RemoteSigned - I think means scripts you write yourself
[tab]AllSigned - the script must be properly signed

So now you can run the script - but it will still close as soon as it is finished.

You need to create you own Pause function:

Code:
function Pause ($Message=”Press any key to continue…”)
 {
 Write-Host -NoNewLine $Message
 $null = $Host.UI.RawUI.ReadKey(”NoEcho,IncludeKeyDown”)
 Write-Host ""
 }

The Pause function must precede (in the script file) any code that uses it, e.g.

Code:
function Pause ($Message=”Press any key to continue…”)
 {
 Write-Host -NoNewLine $Message
 $null = $Host.UI.RawUI.ReadKey(”NoEcho,IncludeKeyDown”)
 Write-Host “”
 }

dir
pause

I called the file [tt]TextFile.ps1[/tt] and placed in my root directory on drive C: so ...

Code:
Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Dim TempPath As String
    'TempPath = My.Computer.FileSystem.SpecialDirectories.Temp
    'Dim FILE_NAME As String = TempPath & "\InstallPreReqs.ps1"
    'OpenPowerShell(FILE_NAME)
    OpenPowerShell("c:\textfile.ps1")

  End Sub

  Private Sub OpenPowerShell(ByVal f As String)
    Dim startInfo As New ProcessStartInfo
    startInfo.FileName = "PowerShell.EXE"
    startInfo.Arguments = f
    Process.Start(startInfo)
  End Sub

End Class

displays the directory of my app's bin\debug folder and waits with the Pause message.

Additionally you must specify the full path to your script.

One further thing, I think when you change the ExecutionPolicy you need to run PowerShell as Administrator.


I told you it wasn't intuitive!! [wink]


PS: I didn't work all this out myself. I had a similar situation a few years ago and managed to track down these elements on the web. Unfortuately I can't credit the authors because I have no idea who they are and I very rarely use PowerShell these days.
 
I want to thank you for your efforts Softhemc. I think I found the same references as you did prior to posting. I found a much simpler solution which was to simply add a line at the end of my script code that simply says "Read-Host" which makes PowerShell wait for input.

I was really hoping for a more robust solution that would allow me to run additional commands in that PowerShell process if there were errors. Instead what I think I need to do is to enable PowerShell transcription which will write the contents of the commands given and their results to a text file for later analysis.

It is so frustrating that there is only one reference I can find on incorporating PowerShell with VB.Net, everything else is in C#.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top