Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forumguidelines
jesper
1.9k
89
45.2k
Powershell, edit string to datetime
Jun 10 2020 3:36 PM
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
Reply
Answers (
1
)
Creating a new code first approach to the existing Database
c# selenium in window application