Home > Mobile >  How to Split Multiple Strings/Lines into Two Variables
How to Split Multiple Strings/Lines into Two Variables

Time:02-02

I am attempting to setup a script where it looks inside a file, retrieves the user description, the username, and the date/time that each of the users logged out. From there I would like to export the information into a .csv file. I was able to successfully retrieve the user description & username cleanly, but I am trying to retrieve the date/time so that it can easily be reviewed as well. To do this, I would like to split the value(s) of the date/time so that one variable is just the dates and the other variable is just the times. From there, I should be able to easily export the information to a csv file in 4 columns (user description, username, logout date, & logout time).

Here is the part of the script I'm having an issue with:

#Create Variable for User Description & User ID & Last Logout
[xml]$UserInfoPath = gc $AccountListPath
$UserDescString = $UserInfoPath.AccountList.UserAccount.description
$UserInfoString = $UserInfoPath.AccountList.UserAccount.UserInfo.username
$UserLogoutString = $UserInfoPath.AccountList.UserAccount.UserInfo.LastLogout
$UserLogoutDate,$UserLogoutTime = $UserLogoutString.split("T")

This is the output of the $UserLogoutString variable:

enter image description here

I would like to use the 'T' to split the string on each of the lines so that the $UserLogoutDate variable displays this:

2022-11-22
2023-02-01
2022-11-22
2022-12-15
2022-12-02

I would like to us the 'T' to split the string on each of the lines so that the $UserLogoutDate variable displays this:

11:06:34.311-06:00
11:09:30.903-06:00
11:06:34.311-06:00
13:15:17.003-06:00
16:10:03.179-06:00

The variables above, however, are not displaying the way I want them to. I am currently seeing this output:

enter image description here

CodePudding user response:

It's unclear if $UserLogoutString is actually a string or already an array.

Assuming this is an array, -split '\r?\n' wouldn't be needed in code below.

$result = $UserLogoutString -split '\r?\n' | ForEach-Object {
    $date, $time = $_.Split('T')
    @{ $date = $time }
}

$($result.Keys)   # has all the dates
$($result.Values) # has all the times

The following would result in an array of hash tables, then via member access enumeration we can reference all .Keys and .Values in the array.

  • Related