Advertisement
K_Wall_24

RDPGate

Jun 3rd, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #######################################
  2. # Name:    Kyle Kowalsky              #
  3. # Date:    May 20, 2016               #
  4. # Purpose: RDP Session Info           #
  5. # Desc:    Get the user and hostname, #
  6. #          start, total, idle, and    #
  7. #          idle total times           #
  8. #######################################
  9.  
  10. # some global variables
  11. $server = "mwSQL-M1"
  12. $database = "User_Monitor"
  13. $table = "RDP_Session_Logs"
  14.  
  15. $session = Get-WmiObject -class "Win32_TSGatewayConnection" -namespace "root\cimv2\TerminalServices" -ComputerName mwRDPGate-M1 -Authentication 6
  16.  
  17. foreach($item in $session) {
  18.    
  19.     $tunnelID = $item.TunnelId
  20.  
  21.     $userName = $item.UserName
  22.  
  23.     $hostName = $item.ConnectedResource
  24.  
  25.     $startTime = ($item.ConvertToDateTime($item.ConnectedTime)).DateTime
  26.  
  27.     # Rip apart the ConnectionDuration so we can format it correctly in DD:HH:MM:SS
  28.     $totalTime = $item.ConnectionDuration
  29.         $totalD = $totalTime.Substring(6,2)
  30.         $totalH = $totalTime.Substring(8,2)
  31.         $totalM = $totalTime.Substring(10,2)
  32.         $totalS = $totalTime.Substring(12,2)
  33.  
  34.         #Proper format
  35.         $totalTime = $totalD+":"+$totalH+":"+$totalM+":"+$totalS
  36.    
  37.     # Do the same to the IdleTime
  38.     $idleTime = $item.IdleTime
  39.        
  40.         # the current idle squad
  41.         $idleH = $idleTime.Substring(8,2)
  42.         $idleHs = [int]$idleH*3600
  43.         $idleM = $idleTime.Substring(10,2)
  44.         $idleMs = [int]$idleM*60
  45.         $idleS = $idleTime.Substring(12,2)
  46.         $idleS = [int]$idleS
  47.  
  48.         # Variables for both math and format versions
  49.         $idleMath = $idleHs+$idleMs+$idleS
  50.  
  51.         $idleTime = $idleH+":"+$idleM+":"+$idleS
  52.  
  53.     # If the session was previously in SQL, grab the old IdleTime and break it down
  54.     $prevIdle = (Invoke-Sqlcmd -database $database -serverinstance $server -query "SELECT Top 1 [Idle_Time] AS prevIdle FROM $table WHERE Username = '$userName' AND Hostname = '$hostName' AND Session_Start = '$startTime'").prevIdle
  55.  
  56.             # the previous idle squad
  57.     if ($prevIdle -ne "000:00:0" -or $prevIdle -ne "00:00:00" -or $prevIdle -ne "000:0:00") # if there is a previous time
  58.         {
  59.             if ($prevIdle -like "*:?") # If seconds are a single digit
  60.             {
  61.                 $prevIdle = $prevIdle.PadLeft(8,"0")
  62.                 $prevIdle = $prevIdle -replace "-", "" # remove any minus signs
  63.  
  64.                 $pIdleH = $prevIdle.Substring($prevIdle.length-8,3)
  65.                 $pIdleHs = [int]$pIdleH*3600 # Convert to seconds for calculation only
  66.                 #
  67.                 $pIdleM = $prevIdle.Substring($prevIdle.length-4,2)
  68.                 $pIdleMs = [int]$pIdleM*60 # Convert to seconds for calculation only
  69.                 #
  70.                 $pIdleS = $prevIdle.Substring($prevIdle.length-1,1)
  71.                 $pIdleS = $pIdleS.PadLeft(2,"0") # Pad the seconds to two digits
  72.                 $pIdleS = [int]$pIdleS
  73.             }
  74.             elseif ($prevIdle -like "*:?:*") # If minutes are a single digit
  75.             {
  76.                 $prevIdle = $prevIdle.PadLeft(8,"0")
  77.                 $prevIdle = $prevIdle -replace "-", ""
  78.  
  79.                 $pIdleH = $prevIdle.Substring($prevIdle.length-8,3)
  80.                 $pIdleHs = [int]$pIdleH*3600
  81.                 #
  82.                 $pIdleM = $prevIdle.Substring($prevIdle.length-4,1)
  83.                 $pIdleM = $pIdleM.PadLeft(2,"0")
  84.                 $pIdleMs = [int]$pIdleM*60
  85.                 #                
  86.                 $pIdleS = $prevIdle.Substring($prevIdle.length-2,2)
  87.                 $pIdleS = [int]$pIdleS
  88.             }
  89.             elseif ($prevIdle -notlike "*:?" -or $prevIdle -notlike "*:?:*")
  90.             {
  91.                 $prevIdle = $prevIdle.PadLeft(8,"0")
  92.                 $prevIdle = $prevIdle -replace "-", ""
  93.  
  94.                 $pIdleH = $prevIdle.Substring($prevIdle.length-8,2)
  95.                 #
  96.                 $pIdleHs = [int]$pIdleH*3600
  97.                 $pIdleM = $prevIdle.Substring($prevIdle.length-5,2)
  98.                 #
  99.                 $pIdleMs = [int]$pIdleM*60
  100.                 $pIdleS = $prevIdle.Substring($prevIdle.length-2,2)
  101.                 #
  102.                 $pIdleS = [int]$pIdleS
  103.             }
  104.         } else # if not, make one
  105.         {
  106.             $pIdleHs = '00'
  107.             $pIdleMs = '00'
  108.             $pIdleS = '00'
  109.         }            
  110.        
  111.         # one comparison int
  112.         $pIdleMath = $pIdleHs+$pIdleMs+$pIdleS
  113.  
  114.    
  115.     # Get the last Total Idle Time from SQL, and break it down into DD MM HH SS
  116.     $pTIdle = (Invoke-Sqlcmd -database $database -serverinstance $server -query "SELECT Top 1 [Total_Idle_Time] AS pTIdle FROM $table WHERE Username = '$userName' AND Hostname = '$hostName' AND Session_Start = '$startTime'").pTIdle
  117.  
  118.         # the previous total idle squad
  119.         if ($pTIdle) # if there is a previous time
  120.         {
  121.             $pTIdle = $pTIdle.PadLeft(8,"0")
  122.             $pTIdle = $pTIdle -replace "-", ""
  123.  
  124.             $pTIdleH = $pTIdle.Substring($pTIdle.length-8,2)
  125.             $pTIdleHs = [int]$pTIdleH*3600
  126.             $pTIdleM = $pTIdle.Substring($pTIdle.length-5,2)
  127.             $pTIdleMs = [int]$pTIdleM*60
  128.             $pTIdleS = $pTIdle.Substring($pTIdle.length-2,2)
  129.             $pTIdleS = [int]$pTIdleS
  130.         } else # if not, make one
  131.         {
  132.             $pTIdleH = '00'
  133.             $pTIdleM = '00'
  134.             $pTIdleS = '00'
  135.         }
  136.  
  137.         # Make them integers for comparison
  138.         $idleMath = [int]$idleMath
  139.         $pIdleMath = [int]$pIdleMath
  140.  
  141.         # This IF statement checks what to do with idleMath before calculating the total idle time
  142.         if ($pIdleMath -gt $idleMath) # if the idle timer has reset
  143.             {
  144.        
  145.                 # If there are fewer new seconds than old seconds (like new: 03:40:12 and old: 02:40:16) it'll mess up calculations by resulting in negative numbers. This removes a minute and adds 60 seconds to avoid the problem
  146.                 if ($idleS -lt $pIdleS)
  147.                 {
  148.                     $idleM -= 1
  149.                     $idleS += 60
  150.                 }
  151.                 # Adds Seconds, rolls over to minutes as needed, adds leading zeroes
  152.                 $tIdleS = [int]$idleS+[int]$pIdleS
  153.                             while ($tIdleS -gt 60)
  154.                                 {
  155.                                     $tIdleM += 1
  156.                                     $tIdleS -= 60
  157.                                 }
  158.                 if ($tIdleS -lt 0)
  159.                     {
  160.                         $tIdleS *= -1
  161.                     }
  162.                 $tIdleS = $tIdleS.ToString("00")
  163.                                        
  164.                 # Minutes
  165.                 # if new is less than old, make it greater
  166.                 if ($idleM -lt $pIdleM)
  167.                 {
  168.                     $idleH -= 1
  169.                     $idleM += 60
  170.                 }
  171.                 $tIdleM = [int]$idleM+[int]$pIdleM
  172.                             while ($tIdleM -gt 60)
  173.                                 {
  174.                                     $tIdleH += 1
  175.                                     $tIdleM -= 60
  176.                                 }
  177.                 if ($tIdleM -lt 0)
  178.                     {
  179.                         $tIdleM *= -1
  180.                     }
  181.                 $tIdleM = $tIdleM.ToString("00")
  182.            
  183.                 # Hours
  184.                 $tIdleH = [int]$idleH+[int]$pIdleH
  185.                 if ($tIdleH -lt 0)
  186.                     {
  187.                         $tIdleH *= -1
  188.                     }
  189.                 $tIdleH = $tIdleH.ToString("00")
  190.                          
  191.             }
  192.         elseif ($idleMath -gt $pIdleMath) # if the user is still Idle
  193.             {
  194.        
  195.                 # Seconds
  196.                 if ($idleS -lt $pIdleS)
  197.                 {
  198.                     $idleM -= 1
  199.                     $idleS += 60
  200.                 }
  201.                 $tIdleS = [int]$idleS-[int]$pIdleS
  202.                         while ($tIdleS -gt 60)
  203.                             {
  204.                                 $tIdleM += 1
  205.                                 $tIdleS -= 60
  206.                             }
  207.                 if ($tIdleS -lt 0)
  208.                     {
  209.                         $tIdleS *= -1
  210.                     }
  211.                 $tIdleS = $tIdleS.ToString("00")
  212.        
  213.                 # Minutes
  214.                 if ($idleM -lt $pIdleM)
  215.                 {
  216.                     $idleH -= 1
  217.                     $idleM += 60
  218.                 }
  219.                 $tIdleM = [int]$idleM-[int]$pIdleM
  220.                         while ($tIdleM -gt 60)
  221.                             {
  222.                                 $tIdleH += 1
  223.                                 $tIdleM -= 60
  224.                             }
  225.                 if ($tIdleM -lt 0)
  226.                     {
  227.                         $tIdleM *= -1
  228.                     }
  229.                 $tIdleM = $tIdleM.ToString("00")
  230.            
  231.                 # Hours
  232.                 $tIdleH = [int]$idleH-[int]$pIdleH
  233.                 if ($tIdleH -lt 0)
  234.                     {
  235.                         $tIdleH *= -1
  236.                     }
  237.                 $tIdleH = $tIdleH.ToString("00")
  238.                            
  239.             }
  240.  
  241.     # Total Idle Seconds
  242.  
  243.     $tIdleS = [int]$tIdleS+[int]$pTIdleS
  244.              while ($tIdleS -gt 60)
  245.                 {
  246.                     $tIdleM += 1
  247.                     $tIdleS -= 60
  248.                 }
  249.     $tIdleS = $tIdleS.ToString("00")
  250.  
  251.     # Total Idle Minutes
  252.     $tIdleM = [int]$tIdleM+[int]$pTIdleM
  253.     $tIdleM = $tIdleM
  254.             while ($tIdleM -gt 60)
  255.                 {
  256.                     $tIdleH += 1
  257.                     $tIdleM -= 60
  258.                 }
  259.     $tIdleM = $tIdleM.ToString("00")
  260.            
  261.     # Total Idle Hours
  262.  
  263.     $tIdleH = [int]$tIdleH+[int]$pTIdleH
  264.  
  265.     $tIdleH = $tIdleH
  266.  
  267.  
  268.     # Adding and Formatting the Total Idle Time
  269.     $totalIdle = [string]$tIdleH+":"+[string]$tIdleM+":"+[string]$tIdleS
  270.  
  271.     $totalIdle = $totalIdle.PadLeft(8,"0")
  272.  
  273.     # Out with the old
  274.     Invoke-sqlcmd -database $database -serverinstance $server -query "DELETE FROM $table WHERE Username = '$userName' AND Hostname = '$hostName' AND Session_Start = '$startTime'"
  275.  
  276.     $cTime = Get-Date
  277.        
  278.     # In with the new
  279.     Invoke-Sqlcmd -database $database -serverinstance $server -query "INSERT INTO $table (Session_ID,Username,Hostname,Session_Start,Session_Time,Idle_Time,Total_Idle_Time,Last_Update) VALUES ('$tunnelID','$userName','$hostName','$startTime','$totalTime','$idleTime','$totalIdle','$cTime')"
  280.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement