blumi

Powershell code in AutoIt

31 posts in this topic

Have read a lot of Powershell topics here, but don't understand everything.

Run a cmd with Powershell in it, run a .ps1 file, or run powershell code.

 

If possible, I don't want to use an external file with code in it.

Just want to put the code in the autoit script.

Here the code

setlocal enableextensions

powershell -command "Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq 'C:\Users\setup' } | Remove-CimInstance"

How to handle this?

Thanks a lot.

Share this post


Link to post
Share on other sites



I do it this way:

$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ...."
$pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I'd also recommend separating items that have quotation requirements, in this case the local path wrapped with single quotes only needs to be passed rather than mucking about with formatting on the line that executes the command.

#include<AutoItConstants.au3>

$sPath = "'C:\Users\" & @UserName & "'"
$sCommand1 = 'powershell -command Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq ' & $sPath & ' }'
$iPID = run($sCommand1, "" , @SW_HIDE , $stdout_child)

$sOutput = ""

     While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ExitLoop
    WEnd


ProcessClose($iPID)

msgbox(0, '' , $sOutput)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
On ‎26‎.‎02‎.‎2016 at 1:49 PM, water said:

I do it this way:

$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ...."
$pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

 

This one works fine, thank you.

Share this post


Link to post
Share on other sites
On ‎26‎.‎02‎.‎2016 at 3:21 PM, iamtheky said:

I'd also recommend separating items that have quotation requirements, in this case the local path wrapped with single quotes only needs to be passed rather than mucking about with formatting on the line that executes the command.

#include<AutoItConstants.au3>

$sPath = "'C:\Users\" & @UserName & "'"
$sCommand1 = 'powershell -command Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq ' & $sPath & ' }'
$iPID = run($sCommand1, "" , @SW_HIDE , $stdout_child)

$sOutput = ""

     While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ExitLoop
    WEnd


ProcessClose($iPID)

msgbox(0, '' , $sOutput)

 

I changed the username but it didn't work.

Share this post


Link to post
Share on other sites

Define: "Does not work". Any errors, any return codes, value of @error?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I got the msbox, but the folder on the hard disk was not deleted.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

you need to put the "remove-ciminstance" back

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

When I want to use this Powershell code

export-startlayout -as bin -path c: \customstartscreenlayout.bin –verbose

export-startlayout -as bin -path c: \customstartscreenlayout.bin –verbose

 

I tried something like this, but it didn't work and I am not sure about the correct syntax.

$sCommand1 = 'powershell -command export-startlayout -as bin -path c:\tmp\customstartscreenlayout.bin  –verbose'
$iPID = run($sCommand1, "" , @SW_SHOW , $stdout_child)

 

Share this post


Link to post
Share on other sites

Any ideas, how to run this code?

Share this post


Link to post
Share on other sites

Either use "ShellExecute" to run your above code or specify the full path to Powershell.exe as I described in post #2.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This is a redirection issue, which cascades it to a rights issue, which presents the user with the error the cmdlet does not exist.

;powershell test
#requireadmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
$sCommand1 = 'Powershell.exe Export-StartLayout -As bin -Path c:\temp\test.bin -verbose'
run($sCommand1 , @SystemDir)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

This might be more roundabout than what has already been suggested, but this small script I wrote to use powershell to defrag all fixed and removable drives shows my method

$drives = DriveGetDrive ( $DT_ALL )
For $i = 1 To $drives[0] Step 1
    If DriveGetType ( $drives[$i] & "\" ) == "Fixed" Or DriveGetType ( $drives[$i] & "\" ) == "Removable" Then
        $letter = StringLeft ( $drives[$i], 1 )
        RunWait ( @ComSpec & ' /c @powershell -c "Optimize-Volume ' & $letter & ' -Verbose"', @SystemDir )
    EndIf
Next

 

Share this post


Link to post
Share on other sites
17 hours ago, iamtheky said:

This is a redirection issue, which cascades it to a rights issue, which presents the user with the error the cmdlet does not exist.

;powershell test
#requireadmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
$sCommand1 = 'Powershell.exe Export-StartLayout -As bin -Path c:\temp\test.bin -verbose'
run($sCommand1 , @SystemDir)

 

Tried this, but I see only for a short time the powershell windows with red font in it, the folder is empty, no test.bin in it.

Did this work for you ?

Share this post


Link to post
Share on other sites
18 hours ago, water said:

Either use "ShellExecute" to run your above code or specify the full path to Powershell.exe as I described in post #2.

Tried something like this, didn't work

$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$para = " export-startlayout -as bin -path c:\tmp\customstartscreenlayout.bin –verbose"
$pid = ShellExecute($sCMD, $para, @SystemDir, "", @SW_MAXIMIZE)

Seems to be more complicated than I thought to use this single line of code in AutoIT

Will there be better powershell support in AutoIT in future?

Share this post


Link to post
Share on other sites
15 hours ago, MattHiggs said:

This might be more roundabout than what has already been suggested, but this small script I wrote to use powershell to defrag all fixed and removable drives shows my method

$drives = DriveGetDrive ( $DT_ALL )
For $i = 1 To $drives[0] Step 1
    If DriveGetType ( $drives[$i] & "\" ) == "Fixed" Or DriveGetType ( $drives[$i] & "\" ) == "Removable" Then
        $letter = StringLeft ( $drives[$i], 1 )
        RunWait ( @ComSpec & ' /c @powershell -c "Optimize-Volume ' & $letter & ' -Verbose"', @SystemDir )
    EndIf
Next

 

Tried this

RunWait ( @ComSpec & ' /c @powershell Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir )

and

RunWait ( @ComSpec & ' /c @powershell -c Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir )

Nothing works

Share this post


Link to post
Share on other sites

Please re-check my post #2. I didn't suggest to use ShellExecute.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
1 minute ago, blumi said:

Tried this

RunWait ( @ComSpec & ' /c @powershell Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir )

and

RunWait ( @ComSpec & ' /c @powershell -c Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir )

Nothing works

Seems you try every possible combination ;) Except the one that works :)

What is @powershell?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Seems you try every possible combination ;) Except the one that works :)

What is @powershell?

I am no expert, but I am sure you know this after reading my posts. ;-)

@powershell, never seen before. It is no macro reference of autoit.

Share this post


Link to post
Share on other sites

Correct, but you are using it in your script (post #18). What do you expect it to do?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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