0

I have following string and I tried the following split mechanism to get the date and the account status but I think my split or index is off.

Write-host $result
@{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512}
$aed =($result -split '=')[0].substring(3)
write-host $aed
ccountExpirationDate - This should be 1/23/2016 6:00:00 PM

$aStatus =($result -split '=')[1].substring(3)
Write-host $aStatus
It should show 512 but it doesn't

=== Added after posting the original code === I tried following code and it works half way. I thin I need to start the same method to get split date and 512 using $rightpath

PS C:\> $position = $result.indexof("=")
PS C:\> $rightpart = $result.substring($position+1)
PS C:\> write-host $rightpart
1/23/2016 6:00:00 PM; userAccountControl=512}
PS C:\>
2
  • Are you sure $result is a string and not something else? Because that's what it looks like to me. Commented Jul 29, 2015 at 17:21
  • yes $result is a string and it contains @{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512} including @ sign Commented Jul 29, 2015 at 17:22

1 Answer 1

1

Your split operations don't do what you think they do. $result -split '=' splits the string at = into 3 parts:

@{AccountExpirationDate
1/23/2016 6:00:00 PM; userAccountControl
512}

The index access [0] selects the first of these parts (@{AccountExpirationDate), and the .Substring(3) returns the substring from the 4th character (index starts at 0) to the end of the string:

ccountExpirationDate

Same for the other split operation. Index [1] selects the second element from the same array (1/23/2016 6:00:00 PM; userAccountControl), and .Substring(3) returns the substring from the 4th character (index starts at 0) to the end of that string:

3/2016 6:00:00 PM; userAccountControl

Demonstration:

PS C:\> $result = "@{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512}"
PS C:\> $result
@{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512}
PS C:\> $a = $result -split '='
PS C:\> $a
@{AccountExpirationDate
1/23/2016 6:00:00 PM; userAccountControl
512}
PS C:\> $a[0].Substring(3)
ccountExpirationDate
PS C:\> $a[1].Substring(3)
3/2016 6:00:00 PM; userAccountControl

If $result is acually a string and not a hashtable you should remove the @{ and } from beginning and end of the string:

$result -replace '^@{|}$'

split it at semicolons to separate the key/value pairs from each other, then split each key/value pair at = to separate key from value.

$pair1, $pair2  = $result -replace '^@{|}$' -split '; '
$null, $aed     = $pair1 -split '=', 2
$null, $aStatus = $pair2 -split '=', 2

The second argument to the -split operator ensures that everything after the first = goes into the second element, even if it contains another = character. The $null on the left side of the assignment discards the first element of the array returned by the split operation (the key name).

Demonstration:

PS C:\> $result = "@{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512}"
PS C:\> $result
@{AccountExpirationDate=1/23/2016 6:00:00 PM; userAccountControl=512}
PS C:\> $pair1, $pair2 = $result -replace '^@{|}$' -split '; '
PS C:\> $null, $aed = $pair1 -split '=', 2
PS C:\> $null, $aStatus = $pair2 -split '=', 2
PS C:\> $aed
1/23/2016 6:00:00 PM
PS C:\> $aStatus
512
Sign up to request clarification or add additional context in comments.

1 Comment

Ansgar: Your code worked like charmed. YOU ROCK. Thanks so much. You saved me from writing 15 lines of code. Thanks Thanks Thanks. Much appreciated.

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.