Hi,
I have this script that takes quser information and format it into an array. I have 1 problem i cant figure out. The IDLE TIME is in a decimal format E.G. 2.57 (HH.MM) but how do i make it a datetime, so i am able to sort on it ?
- $Servers = @(
- "localhost"
- )
- #Initialize $Sessions which will contain all sessions
- [System.Collections.ArrayList]$Sessions = New-Object System.Collections.ArrayList($null)
- #Go through each server
- Foreach ($Server in $Servers) {
- #Get the current sessions on $Server and also format the output
- $DirtyOuput = (quser /server:$Server) -replace '\s{2,}', ',' | ConvertFrom-Csv
- #Go through each session in $DirtyOuput
- Foreach ($session in $DirtyOuput) {
- #Initialize a temporary hash where we will store the data
- $tmpHash = @{}
- #Check if SESSIONNAME isn't like "console" and isn't like "rdp-tcp*"
- If (($session.sessionname -notlike "console") -AND ($session.sessionname -notlike "rdp-tcp*")) {
- #If the script is in here, the values are shifted and we need to match them correctly
- $tmpHash = @{
- Username = $session.USERNAME
- SessionName = "" #Session name is empty in this case
- ID = $session.SESSIONNAME
- State = $session.ID
- IdleTime = $session.STATE
- LogonTime = $session."IDLE TIME"
- ServerName = $Server
- }
- }Else {
- #If the script is in here, it means that the values are correct
- $tmpHash = @{
- Username = $session.USERNAME
- SessionName = $session.SESSIONNAME
- ID = $session.ID
- State = $session.STATE
- IdleTime = $session."IDLE TIME"
- LogonTime = $session."LOGON TIME"
- ServerName = $Server
- }
- }
- #Add the hash to $Sessions
- $Sessions.Add((New-Object PSObject -Property $tmpHash)) | Out-Null
- }
- }
- #Display the sessions, sort by name, and just show Username, ID and Server
- $sessions | Sort Username | select Username, ID, ServerName, IdleTime | FT -AutoSize