Jump to content

PowerShell from Autoit does not save the file


cdeb
 Share

Recommended Posts

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 by cdeb
Link to comment
Share on other sites

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

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 by Nine
Link to comment
Share on other sites

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 by cdeb
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

  • Moderators

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

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

Link to comment
Share on other sites

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

  • Moderators
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

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

  • Moderators

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

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

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