Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. # ------------------------------------------------------------------------
  2. # NAME: Ainsey11 Remote Session Explorer
  3. # AUTHOR: Ainsey11
  4. # DATE:03/09/2015
  5. # WEB : https://ainsey11.com
  6.  
  7. #
  8. # Installation:
  9. # make directory on C:\ called Powershell
  10. # copy contents of the download into this dir. Structure should be as :
  11. #
  12. # C:\
  13. # C:\Powershell\Ainsey11
  14. # C:\Powershell\Ainsey11\Logs
  15. # C:\Powershell\Ainsey11\Logs\Archive
  16. # C:\Powershell\Ainsey11\RDS Session Finder.ps1
  17. # ------------------------------------------------------------------------
  18.  
  19.  
  20. #Import Modules
  21. Import-Module ActiveDirectory
  22.  
  23. #get date
  24. $date = Get-Date -Format HH-mm-dd-MM-yy
  25.  
  26. #cd to right Directory
  27. cd C:\Powershell\Ainsey11\
  28.  
  29. #Move old Log files
  30. Move-Item C:\Powershell\Ainsey11\Logs\*.txt C:\Powershell\Ainsey11\Logs\Archive\$date\
  31.  
  32. Start-Transcript C:\Powershell\Ainsey11\Logs\transaction.log
  33.  
  34. function Show-BalloonTip {
  35.  
  36. [CmdletBinding(SupportsShouldProcess = $true)]
  37. param
  38. (
  39. [Parameter(Mandatory=$true)]
  40. $Text,
  41.  
  42. [Parameter(Mandatory=$true)]
  43. $Title,
  44.  
  45. [ValidateSet('None', 'Info', 'Warning', 'Error')]
  46. $Icon = 'Info',
  47. $Timeout = 10000
  48. )
  49.  
  50. Add-Type -AssemblyName System.Windows.Forms
  51.  
  52. if ($script:balloon -eq $null)
  53. {
  54. $script:balloon = New-Object System.Windows.Forms.NotifyIcon
  55. }
  56.  
  57. $path = Get-Process -id $pid | Select-Object -ExpandProperty Path
  58. $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  59. $balloon.BalloonTipIcon = $Icon
  60. $balloon.BalloonTipText = $Text
  61. $balloon.BalloonTipTitle = $Title
  62. $balloon.Visible = $true
  63.  
  64. $balloon.ShowBalloonTip($Timeout)
  65. }
  66. function getserver {
  67. Show-BalloonTip -Title "A.R.S.E" -Text "Getting Live Servers" -Timeout 10
  68. $servers = Get-ADComputer -Filter {OperatingSystem -Like "*Windows Server*"} -Property * |
  69. ForEach-Object {
  70.  
  71.  
  72. $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
  73. IF($rtn -match 'True') {$_.dnshostname | Out-File -filepath "C:\Powershell\Ainsey11\Logs\LiveServers.txt" -Append
  74. write-host -ForegroundColor green $_.dnshostname is live
  75. }
  76. ELSE { Write-host -ForegroundColor red $_.dnshostname is not live }
  77.  
  78. }
  79. }
  80. function FindRdsSession {
  81. getserver
  82. # Query Active Directory for computers running a Server operating system
  83. $Servers = Get-Content .\Logs\LiveServers.txt
  84. # Loop through the list to query each server for login sessions
  85. ForEach ($Server in $Servers) {
  86. $ServerName = $Server.Name
  87. # Run the qwinsta.exe and parse the output
  88. $queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)
  89. $Servers = "Finding sessions on $Servername"
  90. $form.Update()
  91. # Pull the session information from each instance
  92. ForEach ($queryResult in $queryResults) {
  93. $RDPUser = $queryResult.USERNAME
  94. $sessionType = $queryResult.SESSIONNAME
  95.  
  96. # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number
  97. If (($RDPUser -match "[a-z]") -and ($RDPUser -ne $NULL)) {
  98. $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType
  99.  
  100. }
  101. }
  102. }
  103. }
  104.  
  105. ##############################################
  106. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  107. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  108. [reflection.assembly]::loadwithpartialname("System.Windows.Forms")
  109. [reflection.assembly]::loadwithpartialname("System.Drawing")
  110.  
  111. $TextBox1 = New-Object System.Windows.Forms.TextBox
  112. $TextBox1.Location = New-Object System.Drawing.Point(16, 56)
  113. $TextBox1.Size = New-Object System.Drawing.Size(200, 20)
  114. $TextBox1.TabIndex = 4
  115. $TextBox1.Text = "Enter username here"
  116. $TextBox1.add_TextChanged({TextBox1TextChanged($TextBox1)})
  117.  
  118. $TextBox2 = New-Object System.Windows.Forms.TextBox
  119. $TextBox2.Location = New-Object System.Drawing.Point(16, 88)
  120. $TextBox2.Size = New-Object System.Drawing.Size(200, 20)
  121. $TextBox2.TabIndex = 5
  122. $TextBox2.Text = "Enter domain here"
  123.  
  124. $TextBox3 = New-Object System.Windows.Forms.TextBox
  125. $TextBox3.Location = New-Object System.Drawing.Point(360, 64)
  126. $TextBox3.Multiline = $true
  127. $TextBox3.Size = New-Object System.Drawing.Size(496, 200)
  128. $TextBox3.TabIndex = 7
  129. $TextBox3.Text = "$sessionsonserver"
  130.  
  131. $TextBox4 = New-Object System.Windows.Forms.TextBox
  132. $TextBox4.Location = New-Object System.Drawing.Point(16, 168)
  133. $TextBox4.Multiline = $true
  134. $TextBox4.Size = New-Object System.Drawing.Size(328, 96)
  135. $TextBox4.TabIndex = 9
  136. $TextBox4.Text =
  137.  
  138.  
  139. $Button1 = New-Object System.Windows.Forms.Button
  140. $Button1.Location = New-Object System.Drawing.Point(280, 272)
  141. $Button1.Size = New-Object System.Drawing.Size(96, 23)
  142. $Button1.TabIndex = 1
  143. $Button1.Text = "Find Sessions"
  144. $Button1.UseVisualStyleBackColor = $false
  145. $Button1.BackColor = [System.Drawing.Color]::Green
  146. $Button1.Add_Click({FindRdsSession})
  147.  
  148. $Button2 = New-Object System.Windows.Forms.Button
  149. $Button2.Location = New-Object System.Drawing.Point(464, 272)
  150. $Button2.Size = New-Object System.Drawing.Size(96, 23)
  151. $Button2.TabIndex = 2
  152. $Button2.Text = "Kill Sessions"
  153. $Button2.UseVisualStyleBackColor = $false
  154. $Button2.BackColor = [System.Drawing.Color]::Red
  155. $Button2.Add_Click({KillRdsSession})
  156.  
  157. $Label1 = New-Object System.Windows.Forms.Label
  158. $Label1.Location = New-Object System.Drawing.Point(112, 16)
  159. $Label1.Size = New-Object System.Drawing.Size(576, 23)
  160. $Label1.TabIndex = 0
  161. $Label1.Text = "Welcome to the Ainsey11 Remote Session Explorer, please use the fields below to find what servers you"+[char]39+"re logged into"
  162. $Label1.add_Click({Label1Click($Label1)})
  163.  
  164. $Label2 = New-Object System.Windows.Forms.Label
  165. $Label2.Location = New-Object System.Drawing.Point(360, 40)
  166. $Label2.Size = New-Object System.Drawing.Size(100, 23)
  167. $Label2.TabIndex = 8
  168. $Label2.Text = "You are logged into:"
  169.  
  170. $Label3 = New-Object System.Windows.Forms.Label
  171. $Label3.Location = New-Object System.Drawing.Point(16, 144)
  172. $Label3.Size = New-Object System.Drawing.Size(100, 23)
  173. $Label3.TabIndex = 10
  174. $Label3.Text = "Current Status:"
  175. $Label3.add_Click({Label3Click($Label3)})
  176.  
  177. $Form = New-Object System.Windows.Forms.Form
  178. $Form.ClientSize = New-Object System.Drawing.Size(874, 305)
  179. $Form.Text = "Ainsey11 Remote Session Explorer (Version 1.0)"
  180.  
  181. $Form.Controls.Add($TextBox1)
  182. $Form.Controls.Add($TextBox2)
  183. $Form.Controls.Add($TextBox3)
  184. $Form.Controls.Add($TextBox4)
  185. $Form.Controls.Add($Button1)
  186. $Form.Controls.Add($Button2)
  187. $Form.Controls.Add($Label1)
  188. $Form.Controls.Add($Label2)
  189. $Form.Controls.Add($Label3)
  190. $Form.Add_Shown({$Form.Activate()})
  191. [void] $Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement