I'm trying to use WMI to get the start times of a computer's login sessions using:
$starttimes = Get-WmiObject Win32_LogonSession -ComputerName HM-ITS-KLP |
select starttime
This gives me the date formatted as:
20170120075444.819609+000 (yyyymmddhhmmss.??????+???)
Using the String.ToCharArray() method I managed to convert a string to an array so that I could restructure it better, although in this format it will not accept this as:
Method invocation failed because [Selected.System.Management.ManagementObject] doesn't contain a method named 'ToCharArray'.
Whole code as follows:
$starttimes = Get-WmiObject Win32_LogonSession -ComputerName HM-ITS-KLP |
select StartTime
foreach ($line in $starttimes) {
$dateArray = $line.ToCharArray()
$time = $dateArray[8..9] + ":" + $dateArray[10..11] + ":" + $dateArray[12..13]
$date = $dateArray[6..7] + "/" + $dateArray[4..5] + "/" + $dateArray[0..3]
$LoginTimeAndDate1 = $time + " " + $date
$LoginTimeAndDate = $LoginTimeAndDate1 -join ""
}
DateTimevalues rather than messing around with character arrays.