1

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 ""
}
1

2 Answers 2

5

You forgot to expand the "starttime" property. Try change to this:

foreach ( $line in $starttimes){
$dateArray = $line.starttime.toCharArray()

or this:

foreach ( $line in $starttimes.starttime){
$dateArray = $line.toCharArray()

or this:

$starttimes = Get-WMIObject Win32_LogonSession |  select -Expand starttime
foreach ( $line in $starttimes){
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou very much, solved my problem straight away!
1

try this:

Get-WmiObject Win32_LogonSession -ComputerName "HM-ITS-KLP" | select  @{N='starttime';E={$_.ConvertToDateTime($_.starttime)}}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.