Jump to content

Run Powershell script from Listbox selection


Recommended Posts

Hello,

I've run into several topics about this but none of them seemed to have done it for me.

Long story short, I am creating a GUI that will run my powershell scripts that I use in my every day work.

The idea is to have a listbox with selections:

image.png.d6c61b03f00b90e32863bb9724ed7665.png

Once selected, when the button is clicked, it will run a powershell script  that checks the basic AD information on the computer object and return the output to an input box.

This is my first time using the listbox so I am not sure what I'm doing wrong.

This is the listbox section:

 

; ============ Scripts===========

            Local $ListItem = GUICtrlRead($fList)
            Select
                Case $ListItem = "AD: Computer Object"

                    $ADco =  Run('powershell.exe -executionpolicy bypass -Command (get-adcomputer -Identity & ' $clctHostname & ' -Properties *)', '' , @SW_HIDE , 0x2)
                        Local $ADcoOutput = StdoutRead($ADco)
                        GUICtrlSetData ($fOutput,$ADcoOutput)


                Case $ListItem = "item2"
                    MsgBox(0, "", "Text for Item 2")
            EndSelect

            ; ============ Scripts End ===========

It's probably something silly, but I can't wrap my head around it.

A fresh pair of eyes needed.

 

Thanks in advance!

 

Link to comment
Share on other sites

Hi and Welcome!

What does this output when you run it? Is this your whole script? Can you post the rest of it, or a small-ish section that has the same issues? :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@CaffeinatedAdminThe reason the powershell script is failing is because the syntax is wrong.  It should look like this:

 

; ============ Scripts===========

            Local $ListItem = GUICtrlRead($fList)
            Select
                Case $ListItem = "AD: Computer Object"

                    $ADco =  Run('powershell.exe -executionpolicy bypass -Command "get-adcomputer -Identity ' & $clctHostname & ' -Properties *"', '' , @SW_HIDE , 0x2)
                        Local $ADcoOutput = StdoutRead($ADco)
                        GUICtrlSetData ($fOutput,$ADcoOutput)


                Case $ListItem = "item2"
                    MsgBox(0, "", "Text for Item 2")
            EndSelect

            ; ============ Scripts End ===========

Try writing the command like this.

Link to comment
Share on other sites

  • Moderators

And, just to play devil's advocate here, why go through all of the trouble of getting AutoIt to call PowerShell to do all of this, when AutoIt can do it natively? You should only mix languages if there is a compelling reason to do so.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Just now, JLogan3o13 said:

And, just to play devil's advocate here, why go through all of the trouble of getting AutoIt to call PowerShell to do all of this, when AutoIt can do it natively? You should only mix languages if there is a compelling reason to do so.

@JLogan3o13Because not everyone is as beast a coder as you are.  😁

Link to comment
Share on other sites

  • Moderators

Never claimed that, @MattHiggs. But mixing languages, when it is not an absolute requirement, leads to frustration - a' la what our OP is going through.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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...