1

There is a file which has this content

   SomeText1

   SomeCommand -parameterName abc -login def -password geh

   SomeText2

Could you please advise what PowerShell should be in order to read into the array the variables and values (may be like key/value pair) for

 login=def
 password=geh

The specific about that question is that the order of the login and password parameters may be different, so I need to find the way how to locate the key/value based on the known key name. Also, I know that I need only login and password parameters and associated values.

Thank you very much for your help, in advance!

P.S. I was planning to use the following commands to read the file content, but that can be changed:

$GetFileName = "$env:userprofile\Desktop\Folder\Input.txt" 

$content = [IO.File]::ReadAllText($GetFileName)

2 Answers 2

2

The Select-String cmdlet offers a convenient way to use regular expressions to extract information from files:

$inputFile = "$env:userprofile\Desktop\Folder\Input.txt"

# Extract the information of interest and output it as a hashtable.
# Use $ht = Select-String ... to capture the hashtable in a variable.
Select-String -Allmatches '(?<=-(login|password) +)[^ ]+' $inputFile |
  ForEach-Object {
    foreach ($match in $_.Matches) {
      @{ $match.Groups[1] = $match.Value }
    }
  }

With the sample input, the output is a single hashtable (if multiple lines match, you'll get a hashtable for each line):

Name                           Value
----                           -----
login                          def
password                       geh
  • -AllMatches tells Select-String to search for multiple matches on each line.

  • Regex '(?<=-(login|password) +)[^ ]+' captures the argument associated with parameters -login and -password, while capturing the parameter name in a capture group.

    • Note that the regex assumes that the argument values have no embedded spaces, but that is usually a safe assumption in usernames and passwords.
  • foreach ($match in $_.Matches) processes each match, and constructs and outputs a hashtable (@{ ... } ) whose key is the captured parameter name, and whose value is the captured argument.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use regular expressions to do this. For examle, here is how you can pull the parameters and arguments from a command string and output them as a custom object (for easy manipulation later):

$cmd = "Some-Command -ParameterOne abc -ParameterTwo def -ParameterThree geh -SwitchParameter"

[Regex]::Matches($cmd, "-(?<param>\w+) (?<arg>\w*)|-(?<param>\w+)") |
    ForEach-Object {
        [PsCustomObject]@{
            Parameter = $_.Groups['param'].Value
            Argument = $_.Groups['arg'].Value
        }
    }

The output is like this:

Parameter       Argument
---------       --------
ParameterOne    abc     
ParameterTwo    def     
ParameterThree  geh     
SwitchParameter   

Something like Get-Content might be suitable for reading the command from the file in the first place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.