Jump to content

adding a variable inside a PowerShell command


Go to solution Solved by Nine,

Recommended Posts

Hello,

First of all i'm new and not that good at coding in general, i'm trying to create a simple gui that will achieve a simple goal :

* one input (username) 

* one button (start a powershell with the inputT used) 

So far I have this going : 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

;Creating GUI
GUICreate("search last logon",150,110)
GUICtrlCreateLabel("Entrer un Snumber",30,20,200,30)
$inputSnumber = GUICtrlCreateInput("",25,35,100,20)
$config = GUICtrlCreateButton("Lancer recherche", 25, 60, 100, 20)
$snumber = GUICtrlRead($inputSnumber)

$sPSCmd = "Get-ADUser -Identity" & $snumber &  "-Properties 'LastLogon'| Select-Object Name, @{N='LastLogon'; E {[DateTime]::FromFileTime($_.LastLogon)}}"

;affichage de la fenêtre
GUISetState(@SW_SHOW)

While 1
        $idMsg = GUIGetMsg()
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $config 
                RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -command "&' & $sPSCmd & '"')
                ;trying to find why this does not work ;(
                MsgBox($MB_SYSTEMMODAL,"", GUICtrlRead($inputSnumber))               
                MsgBox($MB_SYSTEMMODAL,"",$sPSCmd)

        EndSwitch
WEnd

I tried this thingy : $sPSCmd = "Get-ADUser -Identity" & $snumber &  "-Properties 'LastLogon'| Select-Object Name, @{N='LastLogon'; E {[DateTime]::FromFileTime($_.LastLogon)}}"

 

with & $snumber & or directly GUICtrlRead($inputSnumber) but, when outputting the command ($sPSCmd) the variable does not shows up.

 

Would someone be kind enough to give this a try and guide me ?

 

Thanks again from a baguette technician 

 

Link to comment
Share on other sites

without knowing
Here is the pattern from

$sPSCmd = "Get-ADUser -Identity" & $snumber &  "-Properties 'LastLogon'|
to
$sPSCmd = "Get-ADUser -Identity " & $snumber &  " -Properties 'LastLogon'|

I know that I know nothing

Link to comment
Share on other sites

Ok thanks guys, I just had to get the input read from inside the while .. my mistake. Now it's working fine.

Just have to find how to get the response from powershell which seems to be an other problem haha

I naively tried to set the command as a variable et print what's inside but no luck of course :

 $response = RunWait(@comspec & ' /c ' & 'powershell.exe -command "&' & $sPSCmd & '"')
 
but of course that does not do the trick.
 
If you still have an idea on this, I will keep locking if someone had the same problem I had
Link to comment
Share on other sites

27 minutes ago, Nine said:

Here one way :

#include <Constants.au3>

Local $iPID = Run(@comspec & ' /c powershell.exe Get-ChildItem -path c:\apps', "", @SW_HIDE, $STDERR_MERGED)
ProcessWaitClose($iPID)

ConsoleWrite(StdoutRead($iPID) & @CRLF)
 

 

Thanks for this solution, but when trying to consolowrite or msgbox it, it's not showing anything.

 

Basically what i'm trying to do is to get the returned last logon and show it in the GUI.

Link to comment
Share on other sites

Just added the -noexit to the command and it's working fine, the command is successful.

 

for the horrible while I have so far, it looks like this : 

 

While 1
        $idMsg = GUIGetMsg()
        $snumber = GUICtrlRead($inputSnumber)
        $sPSCmd = "Get-ADUser -Identity" & " '" & $snumber & "' " &  "-Properties 'LastLogonDate'| Select-Object Name, LastLogonDate"
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $config
                $pwCMD = Run(@comspec & ' /c powershell.exe -NoExit -command  "&' & $sPSCmd)
                ProcessWaitClose($pwCMD)
                msgbox($MB_SYSTEMMODAL,"",StdoutRead($pwCMD) & @CRLF)
        EndSwitch
WEnd
Link to comment
Share on other sites

Thank you for your time, 

 

Just tried you solution but the same extact problem occurs, the msgbox opens empty

 

The GUICtrlRead is working fine and the command also, but when it reaches the msgbox it's empty 😕

Link to comment
Share on other sites

  • Solution

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

×
×
  • Create New...