Jump to content

Parse PowerShell parameters


water
 Share

Recommended Posts

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

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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=company,DC"
$val[1] = "com"

but I need

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

Is this possible?

Thanks in advance!

Edited by water

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

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