Jump to content

Recommended Posts

Posted

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.

Posted

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

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)

 

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

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

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

Posted

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted (edited)

you need to put the "remove-ciminstance" back

Edited by iamtheky

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

Posted

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)

 

Posted

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

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)

 

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

Posted

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

 

Posted
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 ?

Posted
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?

Posted
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

Posted

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted
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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

Posted

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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 (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
×
×
  • Create New...