Short version: I need a way to emulate the Powershell command line parsing within my own function. Something like the .Net System.CommandLine method but with support for Splatting.
Details: I have a text file containing a set of Powershell commands. The file content might look like:
Some-Command -parA "Some Parameter" -parB @{"ParamS"="value"; "ParamT"="value2"}
Some-Command -parA "2nd Parameter" -parB @{"ParamS"="SSS"; "ParamT"="value2"}
As I read each line of the file, I need to transform that line and call a different command with the parameters modified. Taking the first line from above I need to execute Other-Command as if it was called as
Other-Command -parA "Some Parameter" -parB @{"ParamS"="value"; "ParamT"="Other Value"}
(as an aside, these files are generated by me from a different program so I don't need to worry about sanitizing my inputs.)
If someone just entered the Some-Command line above into Powershell, then the parameters would be parsed out, I could access them by name and the splatted parameter would be converted into a hashtable of dictionaries. But, since this is coming from within a text file none of that happens automatically so I'm hoping that there is some commandlet that will do it so I don't have to roll my own.
In my current case I know what all of the parameter names are and what order they will be in the string, so I can just hard code up some string splits to get parameter,value pairs. That still leaves the issue of breaking up the splatted parameter though.
I've looked at ConvertFrom-StringData
, it's similar, but doesn't quite do what I need:
ConvertFrom-StringData -StringData '@{"ParamS"="value"; "ParamT"="value2"}'
Name Value
---- -----
@{"ParamS" "value"; "ParamT"="value2"}
Again, all I'm after in this question is to break this string up as if it was parsed by the powershell command line parser.
Edit: Apparently I wasn't as clear as I could have been. Let's try this. If I call parameterMangle as
parameterMangle -parmA "Some Parameter" -parmB @{"ParamS"="value"; "ParamT"="value2"}
Then there is a clear syntax to modify the parameters and pass them off to another function.
function parameterMangle ($parmA, $parmB)
{
$parmA = $($parmA) + 'ExtraStuff'
$parmB["ParamT"] = 'Other Value'
Other-Command $parmA $parmB
}
But if that same call was a line of text in a file, then modifying those parameters with string functions is very prone to error. To do it robustly you'd have to bring up a full lexical analyzer. I'd much rather find some builtin function that can break up the string in exactly the same way as the powershell command line processor does. That splatted parameter is particularly difficult with all of its double quotes and braces.