Sign in to follow this  
Followers 0
water

Parse PowerShell parameters

7 posts in this topic

A question for the gurus on this forum :graduated:

Our ServiceDesk tool is going to call a compiled AutoIT script. It passes parameters in the format as PowerShell gets them (I was told).

Its either

-xxxx=1234
or

-yyyy="String value"

What do you think is the best way to parse the parameter string and set the variables in AutoIt?

So in my script the variable $xxxx should have the value 1234 and $yyyy should be "String value".

I haven't coded anything yet but would be grateful for any idea on how to start.

Thanks!


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



What about this method?

#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Change2CUI=y
#include <Array.au3>
If Not $CmdLine[0] Then Exit ConsoleWrite("No parameter found!" & @CRLF)
Dim $variables[1]
For $i = 1 To $CmdLine[0]
    $val = StringRegExp( $CmdLine[$i], "-(.*)=(.*)", 3)
    Assign($val[0], $val[1], 2)
    _ArrayAdd($variables, String($val[0]))
Next
$variables[0] = UBound($variables) - 1

For $i = 1 To $variables[0]
    ConsoleWrite($variables[$i] & "=" & Eval($variables[$i]) & @CRLF)
Next

This is just a basic solution without checks whether the parameters are really like -XXXX=YYYY

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Hi UEZ,

vielen Dank!

I had a quick test and it's working perfect. Even embedded blanks are handled correctly:

C:\TEMP>test.exe -xxx="Dies ist ein Test" -yyy=123 -zzz=Normal
xxx=Dies ist ein Test
yyy=123
zzz=Normal

Thanks a lot!


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

Hi UEZ,

vielen Dank!

I had a quick test and it's working perfect. Even embedded blanks are handled correctly:

C:\TEMP>test.exe -xxx="Dies ist ein Test" -yyy=123 -zzz=Normal
xxx=Dies ist ein Test
yyy=123
zzz=Normal

Thanks a lot!

Bitte sehr!

Gruß,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

One small question:

Your RegExp splits at the last "=" in the parameter. Is it possible to split at the first "="?

The parameter

-sOU="OU=Spezial_Policy_Stufe_0,OU=MyCompany,OU=User_Accounts,DC=microsoft,DC=com"
now returns
$val[0] = "sOU=OU=Spezial_Policy_Stufe_0,OU=Konzern,OU=User_Accounts,DC=egv,DC"
$val[1] = "com"
but I need

$val[0] = "sOU"
$val[1] = "OU=Spezial_Policy_Stufe_0,OU=Konzern,OU=User_Accounts,DC=egv,DC=com"

Is this possible?

Thanks in advance!


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

Untested, but try to replace: "-(.*)=(.*)" with "-(.*?)=(.*)"

The question mark will make that group look for the smallest match, instead of the largest.

Share this post


Link to post
Share on other sites

Hi Tvern,

thanks a lot for the fast reply!

Your solution works fine!

Have a nice weekend!


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
Sign in to follow this  
Followers 0