Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ------------------------------------------------------------------------
- # NAME: Ainsey11 Remote Session Explorer
- # AUTHOR: Ainsey11
- # DATE:03/09/2015
- # WEB : https://ainsey11.com
- #
- # Installation:
- # make directory on C:\ called Powershell
- # copy contents of the download into this dir. Structure should be as :
- #
- # C:\
- # C:\Powershell\Ainsey11
- # C:\Powershell\Ainsey11\Logs
- # C:\Powershell\Ainsey11\Logs\Archive
- # C:\Powershell\Ainsey11\RDS Session Finder.ps1
- # ------------------------------------------------------------------------
- #Import Modules
- Import-Module ActiveDirectory
- #get date
- $date = Get-Date -Format HH-mm-dd-MM-yy
- #cd to right Directory
- cd C:\Powershell\Ainsey11\
- #Move old Log files
- Move-Item C:\Powershell\Ainsey11\Logs\*.txt C:\Powershell\Ainsey11\Logs\Archive\$date\
- Start-Transcript C:\Powershell\Ainsey11\Logs\transaction.log
- function Show-BalloonTip {
- [CmdletBinding(SupportsShouldProcess = $true)]
- param
- (
- [Parameter(Mandatory=$true)]
- $Text,
- [Parameter(Mandatory=$true)]
- $Title,
- [ValidateSet('None', 'Info', 'Warning', 'Error')]
- $Icon = 'Info',
- $Timeout = 10000
- )
- Add-Type -AssemblyName System.Windows.Forms
- if ($script:balloon -eq $null)
- {
- $script:balloon = New-Object System.Windows.Forms.NotifyIcon
- }
- $path = Get-Process -id $pid | Select-Object -ExpandProperty Path
- $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
- $balloon.BalloonTipIcon = $Icon
- $balloon.BalloonTipText = $Text
- $balloon.BalloonTipTitle = $Title
- $balloon.Visible = $true
- $balloon.ShowBalloonTip($Timeout)
- }
- function getserver {
- Show-BalloonTip -Title "A.R.S.E" -Text "Getting Live Servers" -Timeout 10
- $servers = Get-ADComputer -Filter {OperatingSystem -Like "*Windows Server*"} -Property * |
- ForEach-Object {
- $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
- IF($rtn -match 'True') {$_.dnshostname | Out-File -filepath "C:\Powershell\Ainsey11\Logs\LiveServers.txt" -Append
- write-host -ForegroundColor green $_.dnshostname is live
- }
- ELSE { Write-host -ForegroundColor red $_.dnshostname is not live }
- }
- }
- function FindRdsSession {
- getserver
- # Query Active Directory for computers running a Server operating system
- $Servers = Get-Content .\Logs\LiveServers.txt
- # Loop through the list to query each server for login sessions
- ForEach ($Server in $Servers) {
- $ServerName = $Server.Name
- # Run the qwinsta.exe and parse the output
- $queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)
- $Servers = "Finding sessions on $Servername"
- $form.Update()
- # Pull the session information from each instance
- ForEach ($queryResult in $queryResults) {
- $RDPUser = $queryResult.USERNAME
- $sessionType = $queryResult.SESSIONNAME
- # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number
- If (($RDPUser -match "[a-z]") -and ($RDPUser -ne $NULL)) {
- $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType
- }
- }
- }
- }
- ##############################################
- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
- [reflection.assembly]::loadwithpartialname("System.Windows.Forms")
- [reflection.assembly]::loadwithpartialname("System.Drawing")
- $TextBox1 = New-Object System.Windows.Forms.TextBox
- $TextBox1.Location = New-Object System.Drawing.Point(16, 56)
- $TextBox1.Size = New-Object System.Drawing.Size(200, 20)
- $TextBox1.TabIndex = 4
- $TextBox1.Text = "Enter username here"
- $TextBox1.add_TextChanged({TextBox1TextChanged($TextBox1)})
- $TextBox2 = New-Object System.Windows.Forms.TextBox
- $TextBox2.Location = New-Object System.Drawing.Point(16, 88)
- $TextBox2.Size = New-Object System.Drawing.Size(200, 20)
- $TextBox2.TabIndex = 5
- $TextBox2.Text = "Enter domain here"
- $TextBox3 = New-Object System.Windows.Forms.TextBox
- $TextBox3.Location = New-Object System.Drawing.Point(360, 64)
- $TextBox3.Multiline = $true
- $TextBox3.Size = New-Object System.Drawing.Size(496, 200)
- $TextBox3.TabIndex = 7
- $TextBox3.Text = "$sessionsonserver"
- $TextBox4 = New-Object System.Windows.Forms.TextBox
- $TextBox4.Location = New-Object System.Drawing.Point(16, 168)
- $TextBox4.Multiline = $true
- $TextBox4.Size = New-Object System.Drawing.Size(328, 96)
- $TextBox4.TabIndex = 9
- $TextBox4.Text =
- $Button1 = New-Object System.Windows.Forms.Button
- $Button1.Location = New-Object System.Drawing.Point(280, 272)
- $Button1.Size = New-Object System.Drawing.Size(96, 23)
- $Button1.TabIndex = 1
- $Button1.Text = "Find Sessions"
- $Button1.UseVisualStyleBackColor = $false
- $Button1.BackColor = [System.Drawing.Color]::Green
- $Button1.Add_Click({FindRdsSession})
- $Button2 = New-Object System.Windows.Forms.Button
- $Button2.Location = New-Object System.Drawing.Point(464, 272)
- $Button2.Size = New-Object System.Drawing.Size(96, 23)
- $Button2.TabIndex = 2
- $Button2.Text = "Kill Sessions"
- $Button2.UseVisualStyleBackColor = $false
- $Button2.BackColor = [System.Drawing.Color]::Red
- $Button2.Add_Click({KillRdsSession})
- $Label1 = New-Object System.Windows.Forms.Label
- $Label1.Location = New-Object System.Drawing.Point(112, 16)
- $Label1.Size = New-Object System.Drawing.Size(576, 23)
- $Label1.TabIndex = 0
- $Label1.Text = "Welcome to the Ainsey11 Remote Session Explorer, please use the fields below to find what servers you"+[char]39+"re logged into"
- $Label1.add_Click({Label1Click($Label1)})
- $Label2 = New-Object System.Windows.Forms.Label
- $Label2.Location = New-Object System.Drawing.Point(360, 40)
- $Label2.Size = New-Object System.Drawing.Size(100, 23)
- $Label2.TabIndex = 8
- $Label2.Text = "You are logged into:"
- $Label3 = New-Object System.Windows.Forms.Label
- $Label3.Location = New-Object System.Drawing.Point(16, 144)
- $Label3.Size = New-Object System.Drawing.Size(100, 23)
- $Label3.TabIndex = 10
- $Label3.Text = "Current Status:"
- $Label3.add_Click({Label3Click($Label3)})
- $Form = New-Object System.Windows.Forms.Form
- $Form.ClientSize = New-Object System.Drawing.Size(874, 305)
- $Form.Text = "Ainsey11 Remote Session Explorer (Version 1.0)"
- $Form.Controls.Add($TextBox1)
- $Form.Controls.Add($TextBox2)
- $Form.Controls.Add($TextBox3)
- $Form.Controls.Add($TextBox4)
- $Form.Controls.Add($Button1)
- $Form.Controls.Add($Button2)
- $Form.Controls.Add($Label1)
- $Form.Controls.Add($Label2)
- $Form.Controls.Add($Label3)
- $Form.Add_Shown({$Form.Activate()})
- [void] $Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement