-
Oct 13th, 2020, 10:35 AM
#1
Thread Starter
New Member
How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-time?
Hey Guys and Gals,
I am executing a PowerShell script from a Visual Basic WinForms UI, and I managed to code it so it executes on a BackgroundWorker thread so that the UI doesn't lock up while the script is running. What it does is imports a .printerExport file to add printers and drivers to a target host, and it runs great. The only issue is that I set the output of the PSObjects to a TextBox and they all get output at the end once the script is completed, rather than output in real time like it would in a PowerShell console window.
I have tried multiple things to get it to output in real-time, but I am just about at my wit's end, and even ReportProgress doesn't manage to get the real-time output as well.
Can someone here help me as to how I can get this done? Thank you so much in advance if anyone can help. Below are the three involved Sub functions I am currently using:
Code:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim HostName As String
Dim PrintName As String
Dim SourceFilePath As String
Dim DestinationPath As String
Dim FileName As String
HostName = txtHostName.Text
PrintName = "\\" & txtHostName.Text
SourceFilePath = txtFilePath.Text
DestinationPath = PrintName & "\c$\Temp\"
If String.IsNullOrEmpty(HostName) Or String.IsNullOrEmpty(SourceFilePath) Then
MessageBox.Show("Please enter the target host name and choose a file path.")
Return
End If
FileName = Path.GetFileName(SourceFilePath)
File.Copy(SourceFilePath, Path.Combine(DestinationPath, Path.GetFileName(SourceFilePath)), True)
Dim PsEnv As New RunspaceInvoke
Dim App As String = $"Invoke-Command -ComputerName {HostName} {{C:\Windows\System32\spool\tools\Printbrm.exe -r -s {PrintName} -f ""C:\Temp\{FileName}""}}"
Dim AppObjects As Collection(Of PSObject) = PsEnv.Invoke(App)
Dim Output As New StringBuilder()
Dim id As Integer = 0
For Each psobj2 As PSObject In AppObjects
id += 1
BackgroundWorker1.ReportProgress(id, psobj2.ToString() & vbCrLf & vbCrLf)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim userState As String = CType(e.UserState, String)
TextBox3.AppendText(userState)
End Sub
Private Sub buttonInstall_Click(sender As Object, e As EventArgs) Handles buttonInstall.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
-
Oct 13th, 2020, 11:54 AM
#2
Re: How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-t
-
Oct 13th, 2020, 12:47 PM
#3
Thread Starter
New Member
Re: How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-t
Originally Posted by jdc2000
Hey jdc, thank you for the reply. I have seen both of those, however the second link I figured wouldn’t be much use as it is on writing the powershell script itself in an async manner, but I am already running it async via the BackgroundWorker thread. The first link I forgot about, and will see if I can figure something out with it. Thank you again for the post.
-
Oct 13th, 2020, 12:50 PM
#4
Thread Starter
New Member
Re: How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-t
Update, the first link is just how to set the output to a TextBox or other control in a GUI application. I am already receiving the output to a TextBox, but the issue is that the output is all at once when the script has finished executing rather than outputting to the TextBox in real-time.
-
Oct 13th, 2020, 01:39 PM
#5
Re: How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-t
-
Oct 13th, 2020, 01:53 PM
#6
Thread Starter
New Member
Re: How do I output from a PShell script (PSObjects?) to a WinForms TextBox in real-t
Originally Posted by jdc2000
Thank you for that second one, looks somewhat promising for the script parts where I call printbrm.exe and can redirect that output. I would just need to figure out how to redirect PowerShell output itself from PowerShell commands such as Add-PrinterDriver in the event where I get a console error such as
Add-PrinterDriver : The specified driver does not exist in the driver store.
At line:1 char:1
+ Add-PrinterDriver -ComputerName LON-LT142564 -Name "Xerox AltaLink C8 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_PrinterDriver:ROOT/StandardCimv2/MSFT_PrinterDriver) [Add-PrinterDri
ver], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070705,Add-PrinterDriver
Thank you for your help so far.
All the best
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|