1

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()
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
Joracosu
  • 309
  • 1
  • 14
  • 2
    Are you trying to combine as many languages and technologies as possible in some kind of frankenstein script? I don't understand why you have a .vbs that executes a .bat, that executes a .ps who calls a .vbs – Geert Bellekens Oct 24 '19 at 11:46
  • XXXXD My intention is to launch a vbs to start a Windows.Form with control in Windows notify task bar. The only way I've found to do it is using PowerShell, and the only way I've found to call a PowerShell from VBScript is using a bat file XDDD. And finally I need to read some parameters from Outlook while the PowerShell is runing, therefore I need to call again to VBScript XXXDD (I'm crying of laughter with your comment). IF THERE IS ANY OTHER WAY OF DOING THIS, I AM OPEN TO NEW IDEAS – Joracosu Oct 24 '19 at 11:56
  • 1
    OK. I found the solution https://stackoverflow.com/a/1437444/7787606 – Joracosu Oct 24 '19 at 12:26
  • I'm pretty sure you can bypass the .bat file pretty easily by executing the .ps directly from vbscript. – Geert Bellekens Oct 24 '19 at 12:36

1 Answers1

0

I found the solution in other question:

Solution here

cscript /nologo myscript.vbs

Joracosu
  • 309
  • 1
  • 14