Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CD $PSScriptRoot
- $global:Location = $PSScriptRoot
- $FormLib = {
- param($Location)
- [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
- [System.Windows.Forms.Application]::EnableVisualStyles()
- $Form = New-Object System.Windows.Forms.Form
- $Timer = New-Object System.Windows.Forms.Timer
- $ConListView = New-Object System.Windows.Forms.ListView
- $Close = New-Object System.Windows.Forms.Button
- $DisconnectAll = New-Object System.Windows.Forms.Button
- $SlideUp = New-Object System.Windows.Forms.Button
- $SlideDown = New-Object System.Windows.Forms.Button
- $TheLabel = New-Object System.Windows.Forms.Label
- function MakeForm {
- param($Location)
- $Global:Location = $Location
- $Filename = $Location
- $Filename += "\Break.txt"
- $break = @('1')
- $break | out-file -FilePath $Filename
- $TotalTime = -1 #in seconds
- $Timer.Interval = 2000
- $Timer.Start()
- $global:TimerTickTracker = '0'
- $Timer_Tick={
- $ScriptDir = $Location
- $ScriptDir += '\ExternalVariables.txt'
- #$listArray = @()
- Get-Content $ScriptDir | ForEach-Object{$listArray += , @($_ -split ",")}
- $ArrayHolder = @()
- foreach ($items in $listArray)
- {
- $itemname = $items[0]
- $itemnumber = $items[1]
- $items = New-Object System.Windows.Forms.ListViewItem($itemname)
- $items.SubItems.Add($itemnumber)
- #$items.Subitems[1].Text = "Disconnect"
- $items.Subitems[1].BackColor = "#FF0000"
- $items.UseItemStyleForSubItems = $False
- $ArrayHolder += $items
- }
- if($ConListView.Items.Count -eq $ArrayHolder.Count -And $global:TimerTickTracker -eq '1'){}
- else
- {$ConListView.Items.Clear()
- $ConListView.Items.AddRange(($ArrayHolder))
- $global:TimerTickTracker = '1'}
- }
- Function DoSomething
- {
- $RemoteName = $ConListView.SelectedItems[0].Text
- $Global:RemoteIP = $ConListView.SelectedItems[0].Subitems[1].Text
- $oReturn=[System.Windows.Forms.MessageBox]::Show("You are about to disconnect $RemoteName"+", are you sure?","Disconnecting",[System.Windows.Forms.MessageBoxButtons]::YESNO)
- switch ($oReturn){
- "YES" {
- $Runner = $Location
- $Runner+= "\cports.exe"
- Start-Process $Runner -ArgumentList "/close * * $Global:RemoteIP *" -Verb runAs
- }
- "NO" {
- }
- }
- }
- $Form.ClientSize = '300,130'
- $Form.text = "Form"
- $Form.BackColor = "#000000"
- $Form.TopMost = $TRUE
- $Form.StartPosition = 2
- $Form.ControlBox = $TRUE
- $Form.Text = "VNC Connections"
- $Form.FormBorderStyle = 'None'
- $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
- $Form.Location = New-Object System.Drawing.Point(750,0)
- $Close.Location = New-Object System.Drawing.Point(210,30)
- $Close.Text = 'Close'
- $Close.BackColor = "#FFC20E"
- $Close.Add_Click({
- $Form.Close()
- $break=0
- $break | out-file -FilePath $Filename
- })
- $DisconnectAll.Location = New-Object System.Drawing.Point(210,55)
- $DisconnectAll.Text = 'D/C All'
- $DisconnectAll.BackColor = "#FFC20E"
- $DisconnectAll.Add_Click({
- $oReturn=[System.Windows.Forms.MessageBox]::Show("This will disconnect all VNC users, are you sure?","Disconnecting",[System.Windows.Forms.MessageBoxButtons]::YESNO)
- switch ($oReturn){
- "YES" {
- Start-Process "C:\Program Files\TightVNC\tvnserver.exe" -ArgumentList "-controlservice -disconnectall"
- }
- "NO" {
- }
- }
- })
- $SlideUp.Location = New-Object System.Drawing.Point(110,100)
- $SlideUp.Visible = $TRUE
- $SlideUp.Text ='Slide Up'
- $SlideUp.BackColor = "#FFC20E"
- $SlideUp.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,-95) $SlideUp.Visible = $false $SlideDown.Visible =$TRUE})
- $SlideDown.Location = New-Object System.Drawing.Point(110,100)
- $SlideDown.Visible = $False
- $SlideDown.Text ='Slide Down'
- $SlideDown.BackColor = "#FFC20E"
- $SlideDown.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,0) $SlideDown.Visible = $false $SlideUp.Visible =$TRUE})
- $ConListView.View = 'Details'
- $ConListView.Width = 180
- $ConListView.Height = 80
- #$ConListView.HeaderStyle = ColumnHeaderStyle.NotClickable
- $ConListView.text = "listView"
- $ConListView.BackColor = "#FFC20E"
- $ConListView.ForeColor = "#FFFFFF"
- #$ConListView.Dock = System.Windows.Forms.DockStyle.Top
- $ConListView.MultiSelect = $False
- $ConListView.Add_ItemActivate({DoSomething})
- $ConListView.Columns.Add("Name", -2)
- $ConListView.Columns.Add("Address", 100)
- $ConListView.FullRowSelect = $True
- $ConListView.Anchor = 'top'
- $ConListView.Location = New-Object System.Drawing.Point(10,10)
- $ConListView.Font = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Regular)
- $ConListView.Items.Add("Loading...")
- $TheLabel.Forecolor = "#FFFFFF"
- $TheLabel.Text = "VS Con Monitor 1.0"
- $TheLabel.Font = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Regular)
- $TheLabel.Location = New-Object System.Drawing.Point(190,105)
- $Form.controls.AddRange(@($ConListView, $Close, $SlideUp, $SlideDown, $TheLabel, $DisconnectAll))
- $Form.Add_Shown({$Form.Activate()})
- $Timer.add_Tick($Timer_Tick)
- $Form.ShowDialog()
- }
- MakeForm $Location}
- $LisLib = {
- param($Location)
- Import-Module NetTCPIP
- $global:allNames = @()
- $global:allNamesAddress = @()
- $global:breaker=1
- function CheckThe-Lines{
- if((Get-NetTCPConnection -LocalPort 5900 | Measure-Object).Count -gt 1)
- {
- $global:stat=(Get-NetTCPConnection -LocalPort 5900 -State Established)
- $global:statc = ($global:stat | Measure-Object).Count
- $global:address = $global:stat[0].RemoteAddress
- $global:allAddress = $global:stat.RemoteAddress
- }
- else {$global:statc=0}
- }
- function defineName {
- $global:name = [System.Net.Dns]::GetHostByAddress($global:address).HostName
- }
- function CallOut-Names {
- Param($from)
- $global:allNames = @()
- foreach ($remotename in $from)
- {
- $nextname = [System.Net.Dns]::GetHostByAddress($remotename).HostName
- $nextaddress = $remotename
- $global:allNames += $nextname.ToLower()+","+$nextaddress
- }
- $lookupTable = @{}
- $ScriptDirNames = $Location
- $ScriptDirNames += '\NetworkNames.txt'
- $global:lineTable = @()
- $lookupTable = Get-Content -raw $ScriptDirNames | ConvertFrom-StringData
- $global:allNames | ForEach-Object {
- $line = $_
- $lookupTable.GetEnumerator() | ForEach-Object {
- if ($line -match $_.Key)
- {
- $line = $line -replace $_.Key, $_.Value
- $global:lineTable += $line
- }
- }
- }
- }
- function UpdateEntries {
- $External_Variables = $Location
- $External_Variables+= "\ExternalVariables.txt"
- $global:lineTable | out-file $External_Variables
- }
- function UpdateEntriesBlank {
- $External_Variables = $Location
- $External_Variables+= "\ExternalVariables.txt"
- Clear-Content $External_Variables
- }
- function MakeToolTip
- {
- [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)
- }
- MakeToolTip "Testing" "VNC Connections" Info
- CheckThe-Lines
- CallOut-Names $allAddress
- UpdateEntries
- $previoustate = $statc
- While($global:breaker -gt 0){
- $ScriptDir = $Location
- $ScriptDir += "\Break.txt"
- $External_Variables = Get-Content -Path $ScriptDir
- $Variable_Checker = $External_Variables | %{$_ -match '1'}
- if($Variable_Checker -contains $TRUE){$global:breaker=1}else{$global:breaker=0}
- $previoustate = $statc
- CheckThe-Lines
- if($statc -gt $previoustate)
- { $lastconnection = $global:lineTable[-1]
- $message = "New Connection From $lastconnection"
- CallOut-Names $allAddress
- UpdateEntries
- MakeToolTip $message "VNC Connections" Info
- }
- if($statc -lt $previoustate)
- {
- $message = "A Connection Closed"
- CallOut-Names $allAddress
- UpdateEntries
- MakeToolTip $message "VNC Connections"
- }
- if($statc -eq 0)
- {
- UpdateEntriesBlank
- }
- Sleep 1
- }
- }
- function Startup {
- $path = $Global:Location
- $path += '\NetworkNames.txt'
- [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
- $title = 'Network Naming Setup'
- if(Test-Path $path -PathType Leaf){
- $job1 = Start-Job $FormLib -Name "FormJob" -ArgumentList $Global:Location
- $null1 = Register-ObjectEvent $job1 -EventName StateChanged -Action {
- if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Completed)
- {
- #Write-Host -ForegroundColor Green 'Event 1 triggering'
- # This command removes the original job
- $sender | Remove-Job -Force
- # These commands remove the event registration
- $eventSubscriber | Unregister-Event -Force
- $eventSubscriber.Action | Remove-Job -Force
- }
- }
- $job2 = Start-Job $LisLib -Name "ListenJob" -ArgumentList $Global:Location
- $null2 = Register-ObjectEvent $job2 -EventName StateChanged -Action {
- if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Completed)
- {
- #Write-Host -ForegroundColor Green 'Event 2 triggering'
- # This command removes the original job
- $sender | Remove-Job -Force
- # These commands remove the event registration
- $eventSubscriber | Unregister-Event -Force
- $eventSubscriber.Action | Remove-Job -Force
- }
- }
- }
- else{
- $nameList = switch -regex (NET.EXE VIEW) { "^\\\\(?<Name>\S+)\s+" {$matches.Name}}
- $nameList = $nameList.ToLower()
- $nameArray = @{}
- if ($nameList.Count -gt 1){
- foreach($name in $nameList)
- {
- $nameArray.add($name, [Microsoft.VisualBasic.Interaction]::InputBox("What is the name of $name", $title))
- }}
- $outArray = @()
- foreach ($i in $nameArray.keys)
- {
- $outArray += ("{0} = {1}" -f $i, $nameArray[$i])
- }
- $outArray | out-file $path
- Startup
- }
- }
- Startup
Add Comment
Please, Sign In to add comment