Jump to content

PowerShell in Autoit


 Share

Recommended Posts

Hi, I have a problem. I want to use PowerShell with AU3, the following code works perfectly.

RunWait(@ComSpec & " /c Powershell.exe -STA -Command Disable-ComputerRestore -drive " & _addDoubleCuotes(@HomeDrive))

Func _addDoubleCuotes($sString)

    Return '"' & $sString & '"'

EndFunc   ;==>_addDoubleCuotes

But the following code does not work :( "Set-WmiInstance It is not recognized as an internal or external command, operable program or batch file." Of course, they work from the PowerShell console.

RunWait(@ComSpec & " /c Powershell.exe -STA -Command Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'" | Set-WmiInstance -Arguments @{VolumeName = "Data"})
Edited by avechuche
Link to comment
Share on other sites

  • Moderators

@avechuche it appears you have an issue with your placement of quotes in several areas (before DeviceID, and after {VolumeName = "Data", for example). I would suggest changing your RunWait to a ConsoleWrite so you can see where the quotes will be.

"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

I would recommend my syntax for legibility, and you can see where I use a semicolon -vs- pipe within the ps command.  You are calling Set-WMI instance as piped to comspec rather than piped to powershell.

 

 

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

Link to comment
Share on other sites

6 hours ago, JLogan3o13 said:

@avechuche it appears you have an issue with your placement of quotes in several areas (before DeviceID, and after {VolumeName = "Data", for example). I would suggest changing your RunWait to a ConsoleWrite so you can see where the quotes will be.

You're right, it was my typo. Also it does not work. The error is the same.

RunWait(@ComSpec & " /k Powershell.exe -STA -Command Get-WmiObject -Class Win32_LogicalDisk -Filter DeviceID='C:' | Set-WmiInstance -Arguments @{VolumeName = 'Data'}")

 

6 hours ago, iamtheky said:

I would recommend my syntax for legibility, and you can see where I use a semicolon -vs- pipe within the ps command.  You are calling Set-WMI instance as piped to comspec rather than piped to powershell.

 

 

I Try looking at your third example, but fails to make it work.

C:\Windows\system32\cmd.exe /k Powershell.exe -STA -Command Get-WmiObject -Class Win32_LogicalDisk -Filter DeviceID='C:' "| Set-WmiInstance -Arguments @{VolumeName = 'Data'}"

C:\Windows\system32\cmd.exe /k Powershell.exe -STA -Command "Get-WmiObject -Class Win32_LogicalDisk -Filter DeviceID='C:' | Set-WmiInstance -Arguments @{VolumeName = 'Data'}"

C:\Windows\system32\cmd.exe /k "Powershell.exe -STA -Command Get-WmiObject -Class Win32_LogicalDisk -Filter DeviceID='C:' | Set-WmiInstance -Arguments @{VolumeName = 'Data'}"

 

Link to comment
Share on other sites

$sCommands = 'powershell -Command import-module ActiveDirectory; "Get-ADComputer -Filter * | Select -Expand Name"'

3rd example, great choice!  you get to see both pipe delimiters in action

the semicolon pipes 'Get-ADComputer' to powershell, and the pipe pipes Select to Get-ADComputer.

That said the set-wmiinstance command does not work for me in that fashion (even copied from here to make sure http://www.powershellmagazine.com/2012/09/04/pstip-another-way-to-modify-wmi-instance-properties/), but I can get both of the lines to execute without any error.  Filter has strict quotation mark requirements, singles must be singles, doubles must be doubles, from autoit you probably also need to stack them since you are sending them through a couple of interpreters (That is a triple stack of double quotes on deviceid to get it to finally make the trip over).

#requireadmin

$sDrive = "'C:'"
$sDeviceID = '"DeviceID=' & $sDrive & '"'

$sCommand1 = 'Powershell -STA Get-wmiobject -Class Win32_LogicalDisk -Filter ""' & $sDeviceID & '""'
$sCommand2 = "Set-WmiInstance -Arguments @{VolumeName='Data'}"

run("cmd /k " & $sCommand1 & " ; " & $sCommand2)

 

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

Link to comment
Share on other sites

15 minutes ago, iamtheky said:
$sCommands = 'powershell -Command import-module ActiveDirectory; "Get-ADComputer -Filter * | Select -Expand Name"'

3rd example, great choice!  you get to see both pipe delimiters in action

the semicolon pipes 'Get-ADComputer' to powershell, and the pipe pipes Select to Get-ADComputer.

That said the set-wmiinstance command does not work for me in that fashion (even copied from here to make sure http://www.powershellmagazine.com/2012/09/04/pstip-another-way-to-modify-wmi-instance-properties/), but I can get both of the lines to execute without any error.  Filter has strict quotation mark requirements, singles must be singles, doubles must be doubles, from autoit you probably also need to stack them since you are sending them through a couple of interpreters (That is a triple stack of double quotes on deviceid to get it to finally make the trip over).

#requireadmin

$sDrive = "'C:'"
$sDeviceID = '"DeviceID=' & $sDrive & '"'

$sCommand1 = 'Powershell -STA Get-wmiobject -Class Win32_LogicalDisk -Filter ""' & $sDeviceID & '""'
$sCommand2 = "Set-WmiInstance -Arguments @{VolumeName='Data'}"

run("cmd /k " & $sCommand1 & " ; " & $sCommand2)

 

Thank you for continuing to support :) but still does not work :(. I had read the web (www.powershellmagazine.com). There, I learned to use the pipe. See the image.

 

PS.jpg

Link to comment
Share on other sites

That is what it looks like with both commands firing correctly on my machine.  When you execute those two commands in powershell just pasting them in, they work and rename the label on your c: drive to data (i assume)?  I cant get set-wmiinstance to do anything of the sort, but  just started trying the quotes were a biatch.

Edited by iamtheky

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

Link to comment
Share on other sites

Another approach:
When you need to run WMI queries then please have a look at ScriptOMatic (For download please see the download section of the forum).
It generates AutoIt code for you which runs much faster because it eliminates the overhead of loading/initializing PowerShell.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

23 hours ago, water said:

Another approach:
When you need to run WMI queries then please have a look at ScriptOMatic (For download please see the download section of the forum).
It generates AutoIt code for you which runs much faster because it eliminates the overhead of loading/initializing PowerShell.

OFF topic

Is there an overhead when we call PS?

I was looking at converting my stuff to PS its only basic windows automation but i dont want to slow it down

Link to comment
Share on other sites

I once had to use PS to create an Exchange mailbox.
It took ages to run (about 30 seconds) and used a lot of memory (about 100MB).
It is a script running at night in the background so execution time is no issue but memory was (up to 70 instances of the script could get kicked off at the same time). So I had to serialize execution of the scripts.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

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