cdeb Posted August 9, 2021 Share Posted August 9, 2021 (edited) Through an Autoit script, I start a script in PowerShell which in turn saves everything to a text file. Well if I run directly from PowerShell the file 'Process.txt' is saved correctly. [PowerShell code example] $P = Get-Process $P | Out-File -FilePath .\Process.txt Get-Content -Path .\Process.txt But if I use Autoit to start the same PowerShell script no 'Process.txt' file is saved. I have used these two methods but both fail: #1 Local $iReturn = ShellExecuteWait('PowerShell.exe', '"'&@ScriptDir&'\script_get_process_write_file.ps1"') ConsoleWrite('$iReturn: ' & $iReturn & @CRLF) #2 Local $sCMD = '"PowerShell.exe" ' & '"'&@ScriptDir&'\script_get_process_write_file.ps1"' ConsoleWrite('$sCMD := ' & $sCMD & @CRLF) $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD) ConsoleWrite('$pid: ' & $pid & @CRLF) ShellExecuteWait or Run work correctly. Can anyone tell me why this isn't working? Edited August 9, 2021 by cdeb Link to comment Share on other sites More sharing options...
Marc Posted August 9, 2021 Share Posted August 9, 2021 Without trying it by myself: the ps1 file writes to the relative path ".\process.txt" So, try to give the run command the right directory. as working directory or change the .ps1 to use an absolute path like "out-file -FilePath c:\test\process.txt Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL) Link to comment Share on other sites More sharing options...
Nine Posted August 9, 2021 Share Posted August 9, 2021 (edited) Did you enable script execution ? Try using : $pid = Run(@ComSpec & " /K " & $sCMD, @ScriptDir, @SW_SHOW) To see if you got any kind of error... BTW, why do you insist using powershell for such an easy task in AutoIt ? (see ProcessList function) Edited August 9, 2021 by Nine Danp2 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 (edited) 1 hour ago, Nine said: Did you enable script execution ? Try using : $pid = Esegui ( @ComSpec & " /K " & $sCMD , @ScriptDir , @SW_SHOW ) To see if you got any kind of error... BTW, why do you insist using powershell for such an easy task in AutoIt ? (see ProcessList function) Actually ProcessList is not what I use. it was to simplify things. I actually need it for Google Text-to-Speech. I tried as you recommended and it returns: The syntax of the file, directory, or volume name is incorrect the paths are correct, I checked: $sCMD = "PowerShell.exe" "C:\Users\User\...\script_get_process_write_file.ps1" or $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe" "C:\Users\User\...\script_get_process_write_file.ps1" Edited August 9, 2021 by cdeb Link to comment Share on other sites More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 2 hours ago, Marc said: Without trying it by myself: the ps1 file writes to the relative path ".\process.txt" So, try to give the run command the right directory. as working directory or change the .ps1 to use an absolute path like "out-file -FilePath c:\test\process.txt even using the absolute path does not work Link to comment Share on other sites More sharing options...
Nine Posted August 9, 2021 Share Posted August 9, 2021 Well powershell script does work for me launching it from AutoIt. Try to simplify your environment to make it work first. Then you can develop based on a successful setup. Create a folder c:\test. Put those 2 files into the folder. Test.ps1 $P = Get-Process $P | Out-File -FilePath .\Process.txt Test.au3 Local $sCMD = "PowerShell.exe " & @ScriptDir & "\Test.ps1" ConsoleWrite('$sCMD := ' & $sCMD & @CRLF) $pid = Run(@ComSpec & " /K " & $sCMD, @ScriptDir, @SW_SHOW) ConsoleWrite('$pid: ' & $pid & @CRLF) Run it. Working for me. FWIW : you can use $oVox = ObjCreate ("SAPI.spVoice") to create a narration object... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 9, 2021 Moderators Share Posted August 9, 2021 I am with Nine, not sure what isn't working for you. The code below works for me, saving the text file where I would expect and returning the correct PID: Local $iPID = Run("powershell.exe -Command Get-Process | Out-File -FilePath '.\Processes.txt'") ConsoleWrite("iPID: " & $iPID & @CRLF) "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 More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 1 hour ago, Nine said: Well powershell script does work for me launching it from AutoIt. Try to simplify your environment to make it work first. Then you can develop based on a successful setup. Create a folder c:\test. Put those 2 files into the folder. Test.ps1 $P = Get-Process $P | Out-File -FilePath .\Process.txt Test.au3 Local $sCMD = "PowerShell.exe " & @ScriptDir & "\Test.ps1" ConsoleWrite('$sCMD := ' & $sCMD & @CRLF) $pid = Run(@ComSpec & " /K " & $sCMD, @ScriptDir, @SW_SHOW) ConsoleWrite('$pid: ' & $pid & @CRLF) Run it. Working for me. FWIW : you can use $oVox = ObjCreate ("SAPI.spVoice") to create a narration object... I tried your example: Test.ps1 and Test.au3 obtaining this output, by the way it opened the cmd.exe console and not the PowerShell.exe one: C:\Users\...\test\Test.ps1 : Unable to upload file C:\Users\...\test\Test.ps1. Script execution is disabled on your system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170. in-line:1 car:1 + C:\Users\...\test\Test.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : Protection error: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess C:\Users\...\test> if I call in PowerShell: Get-ExecutionPolicy I get: Bypass I'll look into $oVox = ObjCreate ("SAPI.spVoice") Thanks Link to comment Share on other sites More sharing options...
Nine Posted August 9, 2021 Share Posted August 9, 2021 3 hours ago, Nine said: Did you enable script execution ? I asked you that before because you need to set-ExecutionPolicy to unrestricted... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 36 minutes ago, JLogan3o13 said: I am with Nine, not sure what isn't working for you. The code below works for me, saving the text file where I would expect and returning the correct PID: Local $iPID = Run("powershell.exe -Command Get-Process | Out-File -FilePath '.\Processes.txt'") ConsoleWrite("iPID: " & $iPID & @CRLF) nothing to do, I tried this way too, I get the PID but not the output of the file Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 9, 2021 Moderators Share Posted August 9, 2021 6 minutes ago, cdeb said: I get the PID but not the output of the file Then it is definitely something in your environment, as it is working for several others. As Nine has asked a couple times, what is your executionpolicy set to "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 More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 15 minutes ago, JLogan3o13 said: Then it is definitely something in your environment, as it is working for several others. As Nine has asked a couple times, what is your executionpolicy set to I changed it Set-ExecutionPolicy to unrestricted but nothing, it doesn't work. But it is normal that Local $sCMD = "PowerShell.exe " & @ScriptDir & "\Test.ps1" ConsoleWrite('$sCMD := ' & $sCMD & @CRLF) $pid = Run(@ComSpec & " /K " & $sCMD, @ScriptDir, @SW_SHOW) ConsoleWrite('$pid: ' & $pid & @CRLF) open: cmd.exe and not PowerShell.exe ? that that's the problem? Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 9, 2021 Moderators Share Posted August 9, 2021 The code is doing what you tell it to; you are saying open @ComSpec (command window) with " /k " (keep open) and then running your PowerShell command inside that window. Have you tried, as has been suggested above, just cutting out the middle-man and running PowerShell directly? "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 More sharing options...
cdeb Posted August 9, 2021 Author Share Posted August 9, 2021 27 minutes ago, JLogan3o13 said: The code is doing what you tell it to; you are saying open @ComSpec (command window) with " /k " (keep open) and then running your PowerShell command inside that window. Have you tried, as has been suggested above, just cutting out the middle-man and running PowerShell directly? I'll do some testing over the next few days and update you. Thank you so much!!! This is a great community!!!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now