Jump to content

Pass Command to PowerShell


ados
 Share

Recommended Posts

Apologies for asking such a basic question but I've tried many different syntax corrections and I cannot get it to work.

I'm trying to pass Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt to PS and then retrieve the output as well.

The latter I have sorted it just the syntax I cannot get working. I have tried putting it into a the run directly and saving it as a string to a variable to then use within the run but no good. The commands work in PS directly of course, I'm sure its the quoting, note that Sophos Anti-Virus must be double quoted for PS to accept.

 

Here is my latest revision:

$PS = 'powershell -command Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt'

;Get the GUI for Trend
$GUI = Runwait(@ComSpec & " /c " & $PS, "", @SW_hide , $stdout_child)

$Output = StdoutRead ($GUI)
MsgBox (0, "Test", $Output)

 

Link to comment
Share on other sites

@ados

this should work for ya:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
#include <AutoitConstants.au3>
; Script Start - Add your code below here
Local $PS = 'Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt'
$GUI = Run("powershell " & $PS, "", @SW_HIDE, $stdout_child)
;$GUI = Runwait(@ComSpec & " /c " & $PS, "", @SW_SHOW , $stdout_child)
While 1
    $Output = StdoutRead($GUI)
    if $Output <> "" Then MsgBox(0, "Test", $Output)
    if StringLen($Output) > 255 Then ExitLoop
WEnd

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

21 minutes ago, ad777 said:

@ados

this should work for ya:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------
#include <AutoitConstants.au3>
; Script Start - Add your code below here
Local $PS = 'powershell -command Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt'
$GUI = Run("powershell " & $PS, "", @SW_HIDE, $stdout_child)
;$GUI = Runwait(@ComSpec & " /c " & $PS, "", @SW_SHOW , $stdout_child)
While 1
    $Output = StdoutRead($GUI)
    if $Output <> "" Then MsgBox(0, "Test", $Output)
    if StringLen($Output) > 255 Then ExitLoop
WEnd

 

Bugger, didn't work. Causes the script to infinitely wait and you never see a message box.

Removed the loop and it then gives a blank message box.

Additionally, the output txt file is empty but if I run the command within PS it outputs. 😔

Link to comment
Share on other sites

I wasn't able to get it working, but I was able to get message boxes going and was able to read error messages (maybe the error messages might help you). I played with the working directory.  Also, I know you can use /k instead of /c, but I'm not sure what is the difference. Maybe playing with those variables might help.

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
Local $sOutput = ""

$PS = 'powershell -command Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt'
$iPID = Run(@ComSpec & " /c " & $PS, @SystemDir, @SW_MINIMIZE, BitOR($STDERR_CHILD, $STDOUT_CHILD))

 While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
        MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
 WEnd
$sOutput = ''
While 1
    $sOutput &= StderrRead($iPID)
    If @error Then ; Exit the loop if the process closes or StderrRead returns an error.
        ExitLoop
    EndIf
    MsgBox($MB_SYSTEMMODAL, "Stderr Read:", $sOutput)
WEnd

 

Link to comment
Share on other sites

1 hour ago, abberration said:

I wasn't able to get it working, but I was able to get message boxes going and was able to read error messages (maybe the error messages might help you). I played with the working directory.  Also, I know you can use /k instead of /c, but I'm not sure what is the difference. Maybe playing with those variables might help.

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
Local $sOutput = ""

$PS = 'powershell -command Get-wmiobject Win32_Product | where {($_.Name -Cnotlike "Sophos Anti-Virus" )} | Select-object IdentifyingNumber | ft -HideTableHeaders > C:\Temp\output.txt'
$iPID = Run(@ComSpec & " /c " & $PS, @SystemDir, @SW_MINIMIZE, BitOR($STDERR_CHILD, $STDOUT_CHILD))

 While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
        MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
 WEnd
$sOutput = ''
While 1
    $sOutput &= StderrRead($iPID)
    If @error Then ; Exit the loop if the process closes or StderrRead returns an error.
        ExitLoop
    EndIf
    MsgBox($MB_SYSTEMMODAL, "Stderr Read:", $sOutput)
WEnd

 

The select-object failing is indicating that the commands passed are not getting processed by PS correctly due to something in the syntax not working from Autoit to PS.

Link to comment
Share on other sites

@ados  Why don't you get the information you want directly from WMI !  There is no need to go thru PS.  You can then create the report you want easily using AutoIt. Example :

#include <Constants.au3>
#include <Array.au3>

Opt("MustDeclareVars", True)

ReadProduct("Windows Live Writer")

Func ReadProduct($sProductName)
  Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
  Local $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_Product WHERE Name = "' & $sProductName & '"')
  If Not IsObj($colItems) Then Exit MsgBox(0, "", "Not an object")
  If Not $colItems.count Then Exit MsgBox(0, "", "Product not found")
  For $oItem In $colItems
    ConsoleWrite($oItem.Name & @CRLF)
    ConsoleWrite($oItem.InstallDate & @CRLF)
    ConsoleWrite($oItem.IdentifyingNumber & @CRLF)
  Next
EndFunc   ;==>ReadProduct

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...