itnifl
asked on
Viewing output from system.diagnostics.process.start in form
I have started a powershell script via system.diagnostics.process .start("sc ript", "arguments"). I want to show the terminal window, or the output in a Visual Basic form or panel. Is this possible, or is it exstensive work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! I liked your solution the best. The syntax is actually like this:
Dim proc = New Process
proc.StartInfo.CreateNoWin dow = True
proc.StartInfo.FileName = "C:\Windows\System32\Windo wsPowerShe ll\v1.0\po wershell.e xe"
proc.StartInfo.Arguments = "D:\Temp\script.ps1"
proc.StartInfo.RedirectSta ndardOutpu t = True
proc.StartInfo.UseShellExe cute = False
proc.Start()
Dim output As String = proc.StandardOutput.ReadTo End()
proc.WaitForExit()
pnlScriptList.Controls.Add (dynamicLi nks(output , 1, 1))
Dim proc = New Process
proc.StartInfo.CreateNoWin
proc.StartInfo.FileName = "C:\Windows\System32\Windo
proc.StartInfo.Arguments = "D:\Temp\script.ps1"
proc.StartInfo.RedirectSta
proc.StartInfo.UseShellExe
proc.Start()
Dim output As String = proc.StandardOutput.ReadTo
proc.WaitForExit()
pnlScriptList.Controls.Add
1. run the script using System.Diagnostics.Process
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "powershell.exe";
info.Arguments = @"c:\users\administrator\d
Process p =Process.Start(info);
p.WaitForExit();
in this case the powershell console will display the output of the script (besides the command console).
the drawback here is that u need to sign the script in order to run it, which is a bit of a hussle.
if you will try running it you'll get an error, somthiing like "The execution of scripts is disabled on this system. Please see "Get-Help about_signing" for more details."
to sign a powershell script, do the following:
1. Set up to view the Certificates by running mmc.exe and adding the Certificates snap-in (Choose My User Account in the options dialog)
2. run the following in command console:
makecert -n "CN=PowerShell Local Certificate Root" -a sha1 -eku 1.3.6.1.5.5.7.3.3 -r -sv root.pvk root.cer -ss Root -sr localMachine
3. run the following in command console:
makecert -pe -n "CN=PowerShell User" -ss MY -a sha1 -eku 1.3.6.1.5.5.7.3.3 -iv root.pvk -ic root.cer
4. make sure execution policy is enable to everyone, by running this:
Set-ExecutionPolicy AllSigned
5. sign the script:
Set-AuthenticodeSignature <script_file_path> @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]
6. now u can run the script either from console or using Process.Start.
btw, u all the steps i mentioned above can be done using the also by Process.Start, u can generate the certificate once and sign any script u want with the same certificate.
the other option (which is way easier) is to use automation:
u need to add System.Management.Automati
C:\Program Files\Reference Assemblies\Microsoft\Windo
follow the attched code to run the script and display the output in your console (since the output is a string u can display it wherever u want).
no signing is needed so its really a piece of cake :)
Open in new window