Spawning Powershell session from PCAN macro?
Posted: Mon 20. Mar 2017, 14:06
Hi,
I am trying to spawn a powershell program from PCAN VBScript. I have it working in windows VBScript but cannot get it working using the engine in PCAN Explorer. The motivation here is to pop up a dialog to the person creating the trace file so that they can save it to a named network location in a particular format, via a marco, without any user interaction other than selecting some parameters from dropdowns and list boxes.
Perhaps there is a better way to do this. I intended to force run the macro on exit so that the user would have to save the file to the correct location in the correct format.
Here is the listing for the Powershell. Let's call it
Executing this on the Powershell command line works as expected.
Let's now invoke this Powershell from the DOS command line as a vbs. Let's call it
This also works as expected. The idea is that Buttons.ps1 would return the full file name to the macro.
Converting this to a macro for PCAN Explorer...
PCAN-Explorer doesn't like this... giving an error "Line16: Object doesn't support this property or method Shell.Exec'"
I have also tried it this way...
When I run it and PCAN offers to me to edit it, it seems to have automatically changed the WScript.Shell to Shell.Application. I read that perhaps Run rather than Exec might be a better way to do it but Run cannot pass a return value (i.e. filename) whereas Exec can. Run can write to a temp file which the macros could open but that sounds messy.
Same error...
Is it possible to do this? Any insights would be appreciated.
Regards
Liam
I am trying to spawn a powershell program from PCAN VBScript. I have it working in windows VBScript but cannot get it working using the engine in PCAN Explorer. The motivation here is to pop up a dialog to the person creating the trace file so that they can save it to a named network location in a particular format, via a marco, without any user interaction other than selecting some parameters from dropdowns and list boxes.
Perhaps there is a better way to do this. I intended to force run the macro on exit so that the user would have to save the file to the correct location in the correct format.
Here is the listing for the Powershell. Let's call it
Code: Select all
Buttons.ps1
Code: Select all
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$global:x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$global:x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please select a computer:"
$objForm.Controls.Add($objLabel)
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 180
[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Write-Host $x
Let's now invoke this Powershell from the DOS command line as a vbs. Let's call it
Code: Select all
GetFile.vbs
Code: Select all
engine = lcase(mid(WScript.FullName, InstrRev(Wscript.FullName, "\")+1))
if not engine = "cscript.exe" then
MsgBox "launch with cscript.exe"
Wscript.Quit
end if
pscommand = "K:\PcanTrace\Buttons.ps1"
cmd = "powershell.exe -noprofile -command " & pscommand
Set shell = CreateObject("WScript.Shell")
Set executor = shell.Exec(cmd)
executor.StdIn.Close
MsgBox executor.StdOut.ReadAll
Converting this to a macro for PCAN Explorer...
Code: Select all
'FILE DESCRIPTION: Test and sample macros
Option Explicit
Sub TestPowershellExec()
'DESCRIPTION: Force save PCAN Trace file to network drive
Dim pscommand
Dim cmd
Dim Shell
Dim executor
pscommand = "K:\PcanTrace\Buttons.ps1"
cmd = "powershell.exe -noprofile -command " & pscommand
Set Shell = CreateObject("Shell.Application")
Set executor = Shell.Exec(cmd)
executor.StdIn.Close
MsgBox executor.StdOut.ReadAll
End Sub
I have also tried it this way...
Code: Select all
'FILE DESCRIPTION: Test and sample macros
Option Explicit
Sub TestPowershellExec()
'DESCRIPTION: Force save PCAN Trace file to network drive
Dim pscommand
Dim cmd
Dim Shell
Dim executor
Dim engine
'engine = lcase(mid(WScript.FullName, InstrRev(Wscript.FullName, "\")+1))
'MsgBox engine
pscommand = "K:\PcanTrace\Buttons.ps1"
cmd = "powershell.exe -noprofile -command " & pscommand
Set Shell = CreateObject("WScript.Shell")
Set executor = Shell.Exec(cmd)
executor.StdIn.Close
MsgBox executor.StdOut.ReadAll
End Sub
Code: Select all
'FILE DESCRIPTION: Test and sample macros
Option Explicit
Sub TestPowershellExec()
'DESCRIPTION: Force save PCAN Trace file to network drive
Dim pscommand
Dim cmd
Dim Shell
Dim executor
pscommand = "K:\PcanTrace\Buttons.ps1"
cmd = "powershell.exe -noprofile -command " & pscommand
Set Shell = CreateObject("Shell.Application")
Set executor = Shell.Exec(cmd)
executor.StdIn.Close
MsgBox executor.StdOut.ReadAll
End Sub
Is it possible to do this? Any insights would be appreciated.
Regards
Liam