How to execute multiple chained PowerShell commands from VBScript

Started by Keith

You wrote this code for me many years ago, and now I am trying to refine it to search for a specific group that a computer is a member of. I am having issues with the Where-Object.
It works in a PowerShell window, but I cannot figure out what I am missing in the built-in editor.
Const Quote = """"

Dim cmd

cmd = "powershell.exe " & Quote & "Get-ADPrincipalGroupMembership (Get-ADComputer " & Input.Column("OS PC Name") & ") | Select-object name" & quote
Where-Object  (name -like "FM Microsoft Bitlocker") | Sort Name

'Create process object and execute PowerShell
Set proc = Internal.CreateObject("process")
proc.execute(cmd)

'Check process exit code
If proc.ExitCode = 0 Then

  'Traverse output lines
  For Each line in proc.output
    Output.Write Line
  Next
Else
  Output.Write "Exit code " & proc.ExitCode
End If
Thanks
SoftPerfect Support forum - Andrew avatar image

Re: How to execute multiple chained PowerShell commands from VBScript   12 August 2024, 11:10

It can be a bit complicated to correctly handle all the nested quotes and redirections when manually constructing a string for execution. Instead, I recommend saving your script as a separate file:
param(
[string]$PcName
)
Get-ADPrincipalGroupMembership (Get-ADComputer $PcName) | Where-Object { $_.Name -like "FM Microsoft Bitlocker" } | Select-Object Name | Sort-Object Name
This script takes a single argument, which is the PC name, and prints out the group memberships. You can then execute this script from VBScript as follows:
cmd = "powershell.exe -ExecutionPolicy Bypass -File C:\path\to\your\script.ps1 -PcName " & Input.Column("OS PC Name")

'Create process object and execute PowerShell
Set proc = Internal.CreateObject("process")
proc.execute(cmd)

'Check process exit code
If proc.ExitCode = 0 Then

  'Traverse output lines
  For Each line in proc.output
    Output.Write Line
  Next
Else
  Output.Write "Exit code " & proc.ExitCode
End If

Reply to this topic

Sometimes you can find a solution faster if you try the forum search, have a look at the knowledge base, or check the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • Write in English. If possible, check your spelling and grammar.

Author:

Email:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

 ********  **     **  **     **  **    **  **     ** 
    **     **     **  **     **   **  **   ***   *** 
    **     **     **  **     **    ****    **** **** 
    **     **     **  **     **     **     ** *** ** 
    **     **     **  **     **     **     **     ** 
    **     **     **  **     **     **     **     ** 
    **      *******    *******      **     **     ** 

Message: