I am trying to develop a form from PowerShell that controls and monitors the execution of a script (in my case VBScript but it could be any other) and get some values from it.
I am new to PowerShell so I apologize about the code structure. English is not my mother tongue so I also apologize for any mistakes. I have also posted another question related to this program development
I am using PowerShell because I want my form to have an icon and a menu in the Windows notification task bar. At this stage the only thing I have is the icon but I will also have a menu. This is not a problem.
I need my form, after Button Click, to launch a VBScript that ends with a result (integer or string) and then assign that result to a PowerShell variable. But what I got is long sentence with the PowerShell copyright before the value of the desired output.
Does anyone know how can I solve my problem? The main purpose is to get the result of a VBScript into a PowerShell variable...
As a test, the VBScript I am trying to launch with my PowerShell program is a simple variable asignation that I called hello.vbs
:
Master_Variable = 222
WScript.Echo Master_Variable
To do so, I've created 3 code files. It starts with a VBScript named 'program.vbs' that calls PowerShell command:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\XXX\Desktop\Program\program.bat" & Chr(34), 0
Set WshShell = Nothing
Secondly it executes a batch file named program.bat
that calls PowerShell Script:
powershell -executionpolicy bypass -file "C:\Users\XXX\Desktop\Program\program.ps1"
Finally the MAIN Script named program.ps1
creates the form with the control and monitor instructions:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Label1 = New-Object System.Windows.Forms.Label
$ComButt1 = New-Object System.Windows.Forms.Button
$Form.Text = "Application"
$Form.Controls.Add($Label1)
$Form.Controls.Add($ComButt1)
$Form.Width = 1260
$Form.Height = 120
$Form.FormBorderStyle = "FixedSingle"
$Form.MaximizeBox = $False
$Form.StartPosition = "CenterScreen"
$Label1.Text = "Value of vbs variable: "
$Label1.Left = 30
$Label1.Top = 20
$Label1.AutoSize = $True
$ComButt1.Text = "Read VBScript"
$ComButt1.Left = 12
$ComButt1.Top = 40
$ComButt1.Width = 102
$ComButt1.Add_Click({
$answer = cscript /nologo "C:\Users\u36557\Desktop\Programita\hello.vbs"
$Label1.Text = $Label1.Text + $answer
})
$Form.ShowDialog()