Reputation: 11201
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:\>
Upvotes: 0
Views: 37
Reputation: 200203
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
Upvotes: 1