Jump to content

[Solved] How to run Powershell with AutoIt and use Get-RDUserSession command?


Recommended Posts

If I run this code, it works perfectly

$CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-ChildItem',@DesktopDir, @SW_SHOW)

But this code

$CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-RDUserSession',@DesktopDir, @SW_SHOW)

I get this error:

Get-RDUserSession : The term 'Get-RDUserSession' is not recognized as the name of a cmdlet, function, script file, or o
perable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try aga
in.

If I try run the command Get-RDUserSession  in normal PowerShell (started from windows start menu) the command works perfectly.

But If I run with AutoIt I get the above mentioned error .

Any ideas?

Edited by DannyJ
Link to post
Share on other sites

I had a similar problem with Active Directory. You need to install those cmdlets.
https://stackoverflow.com/questions/52876847/get-rdusersession-is-not-recognized-as-the-name-of-a-cmdlet

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites
3 minutes ago, water said:

I had a similar problem with Active Directory. You need to install those cmdlets.
https://stackoverflow.com/questions/52876847/get-rdusersession-is-not-recognized-as-the-name-of-a-cmdlet

Thank you I have already tried this command

Quote

Import-Module -Name RemoteDesktop -Verbose

The AutoIt PowerShell I don't know why won't allow to import these modules.

 

Can I acces the default PS with Auto It?

Please use this command if you open the PowerShell, and check the commands in it:

 

$CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-RDUserSession',@DesktopDir, @SW_SHOW)

I don't know why this PowerShell (opened by AutoIt) is different from the Windows default PS. 

Link to post
Share on other sites
Posted (edited)
15 hours ago, argumentum said:

...I don't marry to a tool. I use what I find functional. Good luck.

Don't you think I have a more complex issue than this?

I just wrote here a little piece of a complex issue, if you can't help me at least, don't make sarcastic remarks, it won't help....

Thanks

Edited by DannyJ
Link to post
Share on other sites
Posted (edited)
15 hours ago, Subz said:

Did you compile as 64 bit and use #RequireAdmin?  Example:

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Run("PowerShell.exe -noexit Get-RDUserSession")

 

Thank you very much, I am very grateful for your help, your solution works perfectly.

Solution: 
 

#RequireAdmin

#AutoIt3Wrapper_UseX64=y

Need admin to run this command, and 64bit to run smoothly.

Edited by DannyJ
Link to post
Share on other sites
4 hours ago, DannyJ said:

don't make sarcastic remarks, it won't help....

well, your attitude is not friendly nor helpful. If you start a thread, do attend to it by responding honestly, and with a touch of humility if I may say.
I understand that when one has a need for a solution, one goes into a form of desperation of sorts. But jumping around without attending the dialog is not nice nor welcoming to the one attempting to aid.

Glad to see that you posted the solution to the problem for the next one needing such solution.

...and as I kindly ask everyone that found a clear solution, do prefix [Solved] to the tittle, as this helps future searches.

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By WhaleJesus
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> ; Create Data Folder if it doesn't exist yet If FileExists(@ScriptDir & "\Data") Then Else ShellExecute(@ScriptDir) DirCreate(@ScriptDir & "\Data") EndIf ; Playlist Name & location input Global $playlistnameinput = InputBox("Playlist", "Enter The playlist name", _ "Name") Global $playlistlocationinput = InputBox("Location", "Specify where you would like the playlist folder to be stored", @ScriptDir & "\Playlists\" & $playlistnameinput) ; Create file in Data folder and other vars Global $sDataFile = @ScriptDir & "\Data\Data.txt" Global $DataHandle = FileOpen($sDataFile, 1) Global $DataFileLine = FileReadLine($sDataFile, 1) FileClose($DataFileLine) MsgBox(0, "", $DataFileLine, 10) ; Prove it exists If FileExists($sDataFile) Then _FileWriteToLine($DataHandle, $DataFileLine, $playlistnameinput, True, True) $DataFileLine += 1 _FileWriteToLine($DataHandle, 1, $DataFileLine, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sDataFile & "Does not exist") EndIf Global $sPDataFile = @ScriptDir & "\Data\" & $playlistnameinput & "_Data.txt" Global $PDataHandle = FileOpen($sPDataFile, 1) If FileExists($sPDataFile) Then _FileWriteToLine($PDataHandle, 1, $playlistnameinput, True, True) _FileWriteToLine($PDataHandle, 2, $playlistlocationinput, True, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sPDataFile & "Does not exist") EndIf _FileWriteToLine stopped working and i don't know what it is in my code that's causing this, please help
    • By DannyJ
      $sCommands1 = 'powershell.exe Get-ChildItem' $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2) $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) ConsoleWrite("$sOutput") ConsoleWrite($sOutput) ConsoleWrite(@CRLF) $aOutput = stringsplit($sOutput ,@LF , 2) For $i=0 To  UBound($aOutput) - 1 Step 1     ConsoleWrite($aOutput[$i]) Next The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.
      I have already read some forum answers here, and I have already tried these commands:
       
      Are there any way to use the $aOutput variable like in PowerShell:
      PowerShell:
      $a = Get-ChildItem $a.Mode I imagine this in AutoIt  $aOutput
      ConsoleWrite($aOutput[i].Mode) Or if I split this command into 2 dimension like:
      For $i To UBound($aOutput)-1 Step 1 ConsoleWrite($aOutput[$i][1]) ConsoleWrite($aOutput[$i][2]) Next  
    • By Automania
      Hi all,
      I haven't used AutoIt in more than 10 years and I am sure a lot has improved since that long time. I hope you can give me some suggestions on my approach.
      Task: I need to extract user data (for around 1700 users) from a website tool. That tool shows an output in a table on the website. However, no export feature is available and I need the data in an Excel file, such as:
      username, serial number (of a laptop), ID number (of laptop) and some more
       
      With my knowledge from 2009 I would do this:
      1) use _IEextract with each username in the url to get the whole source code of the website with the user's data summary
      2) Work with lots of regexpressions to extract each data piece, save them into variables/array
      3) Write variable values into an Excel file
      4) rinse repeat 1700 times
       
      The relevant line for step 3 looks like this:
      <td class="resultcell"><span class="new">2021-03-23 11:05:00</span></td><td class="resultcell">Hostname-1234</td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=serial%20numbers&criteria=123456">123456</a></td><td class="resultcell">0987654/td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=usernames&criteria=myusername">myusername</a> and so on.. so here it would be Hostname-1234, 0987654 and myusername that I would need to extract.

      Although this may work it does not appear very efficient and would take a while. So I am happy for an alternate approach. Preferably, without using additional exe binary files due to company policies besides AutoIt itself.
    • By SEuBo
      Hi!
      I am just getting started with C and C++. I have created a pretty simple C code which is calling a dll function.
      When I compile and run, I get the appropriate Output. So it works fine.

       
      Now I would want to transform that to AutoIt. -> I would like to call the "RfcOpenConnection" function from AutoIt - but whatever I try with DLLCall, I can not get it to work. 
      Can someone point me in the right direction? DLL, C Sourcecode and compiled exe are attached too large to be attached, so they're uploaded here: 
      https://drive.google.com/file/d/12CUSsISl0mojiMCNxKjps1Sdoox3JlCX/view?usp=sharing
       
      Thanks a bunch!
    • By sudeepjd
      I am looking for a way to pull up a Child GUI Window that users can enter information into and return that information to the main for loop which is running off an array. However, I have been unable to do so because the For loop continues even though the child window is open. If I put in another while loop inside the child window function, I am not able to poll the windows for the events looking for the close button getting clicked.
      I have put together a simple test application that shows this. Any help with holding the main loop while the child window is open and returning when the Close button is clicked is appreciated.
      In the below example, the child window contains a single text box, however, on my main application the Child GUI is much more complex with multiple pieces of information being returned.
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> AutoItSetOption("GUIOnEventMode", 1) Global $hGUI, $hChild Global $childClose = False Global $childValue OpenMainGUI() While Sleep(100) ConsoleWrite("Main Loop") WEnd Func OpenMainGUI() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 400, 100) $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Events GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitMain", $hGUI) GUICtrlSetOnEvent($btnMain, "createChildren") EndFunc ;==>Example Func createChildren() For $i = 1 to 5 createChild($i) ConsoleWrite($i & " - " & $childValue & @CRLF) Next EndFunc Func createChild($valInp) $childClose = False $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI) $txtOperation = GUICtrlCreateInput($valInp, 10, 10, 100, 20) $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30) GUICtrlSetOnEvent($btnCloseChild, "_ExitChild") ; Display Child GUISetState(@SW_SHOW) ;Wait here till Close button is clicked While $childClose = False ConsoleWrite("Stuck in this loop..." & @LF) Sleep(100) $aMsg = GUIGetMsg(1) If $aMsg[0] >0 Then _ArrayDisplay($aMsg) Wend EndFunc Func _ExitMain() Exit EndFunc Func _ExitChild() ConsoleWrite("Exit Child Called") $childValue = GUICtrlRead($txtOperation) $childClose = True GUIDelete($hChild) EndFunc Thanks in advance for any help offered.
      My other alternative is to create a separate EXE for the child window and use ShellExecuteWait to wait for the child window to close before the loop continues, but I am hoping to avoid doing that.
×
×
  • Create New...