Advertisement
K_Wall_24

Powershell-Time

May 20th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Output of ###
  2. Get-WmiObject -class "Win32_TSGatewayConnection" -namespace "root\cimv2\TerminalServices" -ComputerName mwRDPGate-M1 -Authentication 6
  3. ###
  4. ConnectedResource         : ts-p1
  5. ConnectedTime             : 20160520171915.190000-000
  6. ConnectionDuration        : 00000000023717.000000:000
  7. #########
  8.  
  9.  
  10. $server = "mwSQL-M1"
  11. $database = "User_Monitor"
  12. $table = "RDP_Session_Logs"
  13. $lastIdle = $idleTime
  14. $temp = 0
  15.  
  16. $session = Get-WmiObject -class "Win32_TSGatewayConnection" -namespace "root\cimv2\TerminalServices" -ComputerName mwRDPGate-M1 -Authentication 6
  17.  
  18. foreach($item in $session) {
  19.     $cSession = $session[$temp]
  20.     $userName = $cSession.UserName
  21.     $hostName = $cSession.ConnectedResource
  22.     $startTime = ($cSession.ConvertToDateTime($cSession.ConnectedTime)).DateTime
  23.     $totalTime = ($cSession.ConvertToDateTime($cSession.ConnectionDuration)).DateTime
  24.     $idleTime = $cSession.IdleTime
  25.     $guid = [System.GUID]::NewGuid().ToString()
  26.     #Invoke-Sqlcmd -database $database -serverinstance $server -query "insert into $table (Session_ID,Username,Hostname,Session_Start,Session_Time,Idle_Time) VALUES ('$temp','$userName','$hostName','$startTime','$totalTime','$idleTime')"
  27.     write-host $userName $temp $startTime
  28.     $temp = $temp+1
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement