Advertisement
mikedopp

LogOffRemoteUsersGUI

Aug 20th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Remoteapp session logoff
  3. Users that use this script need to be part of the administrators group on the RD Broker server, and I believe any of your session hosts
  4. Also need to have remote server administration tools installed
  5. https://www.microsoft.com/en-us/download/details.aspx?id=45520
  6. Torbar 8-17-18
  7. Thanks to https://poshgui.com for the powershell gui builder
  8. #>
  9.  
  10. #full FQDN of your rdbroker server
  11. $rdbroker="rdbroker.contoso.com"
  12.  
  13. #your domain, so in the sessionhost server column it doesn't show the full FQDN, just the server name
  14. $domain=".contoso.com"
  15.  
  16.  
  17. Add-Type -AssemblyName System.Windows.Forms
  18. [System.Windows.Forms.Application]::EnableVisualStyles()
  19.  
  20.  
  21. #Check to make sure remotedesktop powershell module is installed.  If it is, continue, if not say it needs RSAT and gives you option to download it
  22. if (Get-Module -ListAvailable -Name remotedesktop) {
  23. #all good, go on            
  24. } else {
  25.    
  26.      $msgBoxInput = [System.Windows.Forms.MessageBox]::Show('Remote Desktop module not installed
  27. Download and install Remote Server Administration tools from Microsoft
  28. launch download page?
  29. ', 'Remote desktop', 'YesNo', 'Error')
  30.      switch ($msgBoxInput) {
  31.  
  32.                 'Yes' {Start-Process -file iexplore -arg 'https://www.microsoft.com/en-us/download/details.aspx?id=45520' -PassThru }
  33.  
  34.                 'No' {
  35.                     #Do nothing
  36.                 }
  37.             }
  38.  
  39. exit
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46. #create GUI
  47.  
  48. $Form = New-Object system.Windows.Forms.Form
  49. $Form.ClientSize = '520,438'
  50. $Form.text = "RemoteDesktop Logoff"
  51. $Form.TopMost = $false
  52.  
  53. $ListView1 = New-Object system.Windows.Forms.ListView
  54. $ListView1.text = "listView"
  55. $ListView1.width = 475
  56. $ListView1.height = 236
  57. $ListView1.location = New-Object System.Drawing.Point(23, 64)
  58. $ListView1.View = [System.Windows.Forms.View]::Details
  59. $ListView1.Columns.Add("Session ID", 100) | Out-Null
  60. $ListView1.Columns.Add("Collection", 100) | Out-Null
  61. $ListView1.Columns.Add("User", 150) | Out-Null
  62. $ListView1.Columns.Add("Server", -2) | Out-Null
  63.  
  64. $UsernameLabel = New-Object system.Windows.Forms.Label
  65. $UsernameLabel.text = "Username"
  66. $UsernameLabel.AutoSize = $true
  67. $UsernameLabel.width = 25
  68. $UsernameLabel.height = 10
  69. $UsernameLabel.location = New-Object System.Drawing.Point(26, 23)
  70. $UsernameLabel.Font = 'Microsoft Sans Serif,10'
  71.  
  72. $UsernameTextBox = New-Object system.Windows.Forms.TextBox
  73. $UsernameTextBox.multiline = $false
  74. $UsernameTextBox.width = 100
  75. $UsernameTextBox.height = 20
  76. $UsernameTextBox.location = New-Object System.Drawing.Point(94, 23)
  77. $UsernameTextBox.Font = 'Microsoft Sans Serif,10'
  78. $UsernameTextBox.Text = "*"
  79.  
  80. $SearchButton = New-Object system.Windows.Forms.Button
  81. $SearchButton.text = "Search"
  82. $SearchButton.width = 60
  83. $SearchButton.height = 30
  84. $SearchButton.location = New-Object System.Drawing.Point(209, 17)
  85. $SearchButton.Font = 'Microsoft Sans Serif,10'
  86.  
  87. $EndSessionButton = New-Object system.Windows.Forms.Button
  88. $EndSessionButton.text = "End Session"
  89. $EndSessionButton.width = 111
  90. $EndSessionButton.height = 30
  91. $EndSessionButton.location = New-Object System.Drawing.Point(48, 366)
  92. $EndSessionButton.Font = 'Microsoft Sans Serif,10'
  93.  
  94. $Form.controls.AddRange(@($ListView1, $UsernameLabel, $UsernameTextBox, $SearchButton, $EndSessionButton))
  95.  
  96.  
  97. #logic here
  98. $SearchButton.Add_Click( {  
  99.         $ListView1.Items.Clear()
  100.                
  101.         if ($UsernameTextBox.Text.Length -lt 1) {$UsernameTextBox.Text = '*'}
  102.         $Usersessions = Get-RDUserSession -ConnectionBroker $rdbroker | Sort-Object -Property UserName| where {$_.UserName -like "*" + $UsernameTextBox.text + "*"}
  103.  
  104.         foreach ($UserSession in $Usersessions) {
  105.             $Hostserver = $UserSession.HostServer.Replace("$domain", "")
  106.             $ListViewItem = New-Object System.Windows.Forms.ListViewItem($Usersession.UnifiedSessionID)
  107.             $ListViewItem.Subitems.Add($Usersession.CollectionName)
  108.             $ListViewItem.Subitems.Add($Usersession.Username)
  109.             $ListViewItem.Subitems.Add($Hostserver)
  110.             $ListView1.Items.Add($ListViewItem)
  111.         }
  112.     })
  113. $EndSessionButton.Add_Click( {  
  114.  
  115.         foreach ($item in $ListView1.SelectedItems) {
  116.             $sessionID = $item.SubItems[0].text
  117.             $collectionname = $item.SubItems[1].text
  118.             $username = $item.SubItems[2].text
  119.             $servername = $item.SubItems[3].text
  120.             Write-Host "Session" $sessionID
  121.             Write-Host "Collection" $collectionname
  122.             Write-Host "username" $username
  123.             Write-Host "server" $servername
  124.  
  125.             $msgBoxInput = [System.Windows.Forms.MessageBox]::Show('Log ' + $username + ' off of ' + $servername + '?', 'Error', 'YesNo', 'Error')
  126.  
  127.             switch ($msgBoxInput) {
  128.  
  129.                 'Yes' {Invoke-RDUserLogoff -HostServer $servername -UnifiedSessionID $sessionID -Force}
  130.  
  131.                 'No' {
  132.                     #Do nothing
  133.                 }
  134.             }
  135.           }
  136.         })
  137.     [void]$Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement