avechuche

PowerShell in Autoit

10 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



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


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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.

 

 


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

Share this post


Link to post
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'}"

 

Share this post


Link to post
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)

 


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

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

Share this post


Link to post
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 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
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

Share this post


Link to post
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 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