Guest User

Untitled

a guest
Jun 5th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.21 KB | None | 0 0
  1. CD $PSScriptRoot
  2. $global:Location = $PSScriptRoot
  3.  
  4. $FormLib = {
  5. param($Location)
  6.  
  7. [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")  
  8. [System.Windows.Forms.Application]::EnableVisualStyles()  
  9.  
  10. $Form                            = New-Object System.Windows.Forms.Form
  11. $Timer                           = New-Object System.Windows.Forms.Timer
  12. $ConListView                     = New-Object System.Windows.Forms.ListView
  13. $Close                           = New-Object System.Windows.Forms.Button
  14. $DisconnectAll                   = New-Object System.Windows.Forms.Button
  15. $SlideUp                         = New-Object System.Windows.Forms.Button
  16. $SlideDown                       = New-Object System.Windows.Forms.Button
  17. $TheLabel                        = New-Object System.Windows.Forms.Label
  18.  
  19. function MakeForm {
  20. param($Location)
  21. $Global:Location = $Location
  22.  
  23. $Filename = $Location
  24. $Filename += "\Break.txt"
  25. $break = @('1')
  26. $break | out-file -FilePath $Filename
  27.  
  28. $TotalTime = -1 #in seconds
  29. $Timer.Interval = 2000
  30. $Timer.Start()
  31. $global:TimerTickTracker = '0'
  32.  
  33. $Timer_Tick={
  34.  
  35.    
  36.    
  37.     $ScriptDir = $Location
  38.     $ScriptDir += '\ExternalVariables.txt'
  39.     #$listArray = @()
  40.     Get-Content $ScriptDir | ForEach-Object{$listArray += , @($_ -split ",")}
  41.  
  42.     $ArrayHolder = @()
  43.  
  44.     foreach ($items in $listArray)
  45.     {
  46.     $itemname = $items[0]
  47.     $itemnumber = $items[1]
  48.     $items = New-Object System.Windows.Forms.ListViewItem($itemname)
  49.     $items.SubItems.Add($itemnumber)
  50.     #$items.Subitems[1].Text = "Disconnect"
  51.     $items.Subitems[1].BackColor = "#FF0000"
  52.     $items.UseItemStyleForSubItems = $False
  53.     $ArrayHolder += $items
  54.    
  55.     }
  56.     if($ConListView.Items.Count -eq $ArrayHolder.Count -And $global:TimerTickTracker -eq '1'){}
  57.     else
  58.     {$ConListView.Items.Clear()
  59.     $ConListView.Items.AddRange(($ArrayHolder))
  60.     $global:TimerTickTracker = '1'}
  61. }
  62.  
  63. Function DoSomething
  64. {
  65. $RemoteName = $ConListView.SelectedItems[0].Text
  66. $Global:RemoteIP = $ConListView.SelectedItems[0].Subitems[1].Text
  67.  
  68. $oReturn=[System.Windows.Forms.MessageBox]::Show("You are about to disconnect $RemoteName"+", are you sure?","Disconnecting",[System.Windows.Forms.MessageBoxButtons]::YESNO)
  69.  
  70. switch ($oReturn){
  71.     "YES" {
  72.     $Runner = $Location
  73.     $Runner+= "\cports.exe"
  74.     Start-Process $Runner -ArgumentList "/close * * $Global:RemoteIP *" -Verb runAs
  75.     }
  76.     "NO" {
  77.  
  78.     }
  79. }
  80.        
  81. }
  82.  
  83. $Form.ClientSize                     = '300,130'
  84. $Form.text                           = "Form"
  85. $Form.BackColor                      = "#000000"
  86. $Form.TopMost                        = $TRUE
  87. $Form.StartPosition                  = 2
  88. $Form.ControlBox                     = $TRUE
  89. $Form.Text                           = "VNC Connections"
  90. $Form.FormBorderStyle                = 'None'
  91. $Form.StartPosition                  = [System.Windows.Forms.FormStartPosition]::Manual
  92. $Form.Location                       = New-Object System.Drawing.Point(750,0)
  93.  
  94. $Close.Location                      = New-Object System.Drawing.Point(210,30)
  95. $Close.Text                          = 'Close'
  96. $Close.BackColor                     = "#FFC20E"
  97. $Close.Add_Click({
  98.     $Form.Close()
  99.     $break=0
  100.     $break | out-file -FilePath $Filename
  101. })
  102.  
  103. $DisconnectAll.Location               = New-Object System.Drawing.Point(210,55)
  104. $DisconnectAll.Text                   = 'D/C All'
  105. $DisconnectAll.BackColor              = "#FFC20E"
  106. $DisconnectAll.Add_Click({
  107.  
  108. $oReturn=[System.Windows.Forms.MessageBox]::Show("This will disconnect all VNC users, are you sure?","Disconnecting",[System.Windows.Forms.MessageBoxButtons]::YESNO)
  109.  
  110. switch ($oReturn){
  111.     "YES" {
  112.     Start-Process "C:\Program Files\TightVNC\tvnserver.exe" -ArgumentList "-controlservice -disconnectall"
  113.     }
  114.     "NO" {
  115.  
  116.     }  
  117.    }  
  118. })
  119.  
  120. $SlideUp.Location                    = New-Object System.Drawing.Point(110,100)
  121. $SlideUp.Visible                     = $TRUE
  122. $SlideUp.Text                        ='Slide Up'
  123. $SlideUp.BackColor                   = "#FFC20E"
  124. $SlideUp.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,-95) $SlideUp.Visible = $false $SlideDown.Visible =$TRUE})
  125.  
  126. $SlideDown.Location                  = New-Object System.Drawing.Point(110,100)
  127. $SlideDown.Visible                   = $False
  128. $SlideDown.Text                      ='Slide Down'
  129. $SlideDown.BackColor                 = "#FFC20E"
  130. $SlideDown.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,0) $SlideDown.Visible = $false $SlideUp.Visible =$TRUE})
  131.  
  132. $ConListView.View                    = 'Details'
  133. $ConListView.Width                   = 180
  134. $ConListView.Height                  = 80
  135. #$ConListView.HeaderStyle             = ColumnHeaderStyle.NotClickable
  136. $ConListView.text                    = "listView"
  137. $ConListView.BackColor               = "#FFC20E"
  138. $ConListView.ForeColor               = "#FFFFFF"
  139. #$ConListView.Dock                    = System.Windows.Forms.DockStyle.Top
  140. $ConListView.MultiSelect             = $False
  141. $ConListView.Add_ItemActivate({DoSomething})
  142.  
  143. $ConListView.Columns.Add("Name", -2)
  144. $ConListView.Columns.Add("Address", 100)
  145.  
  146. $ConListView.FullRowSelect           = $True
  147. $ConListView.Anchor                  = 'top'
  148. $ConListView.Location                = New-Object System.Drawing.Point(10,10)
  149. $ConListView.Font                    = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Regular)
  150. $ConListView.Items.Add("Loading...")
  151.  
  152. $TheLabel.Forecolor                  = "#FFFFFF"
  153. $TheLabel.Text                       = "VS Con Monitor 1.0"
  154. $TheLabel.Font                       = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Regular)
  155. $TheLabel.Location                   = New-Object System.Drawing.Point(190,105)
  156.  
  157.  
  158.  
  159. $Form.controls.AddRange(@($ConListView, $Close, $SlideUp, $SlideDown, $TheLabel, $DisconnectAll))
  160. $Form.Add_Shown({$Form.Activate()})
  161. $Timer.add_Tick($Timer_Tick)
  162. $Form.ShowDialog()
  163.     }
  164. MakeForm $Location}
  165.  
  166.  
  167. $LisLib = {
  168. param($Location)
  169.  
  170. Import-Module NetTCPIP
  171.  
  172. $global:allNames = @()
  173. $global:allNamesAddress = @()
  174. $global:breaker=1
  175.  
  176. function CheckThe-Lines{
  177.    
  178.     if((Get-NetTCPConnection -LocalPort 5900 | Measure-Object).Count -gt 1)
  179.     {
  180.         $global:stat=(Get-NetTCPConnection -LocalPort 5900 -State Established)
  181.         $global:statc = ($global:stat | Measure-Object).Count
  182.         $global:address = $global:stat[0].RemoteAddress
  183.         $global:allAddress = $global:stat.RemoteAddress
  184.        
  185.        
  186.     }
  187.     else {$global:statc=0}
  188. }
  189.  
  190. function defineName {
  191. $global:name = [System.Net.Dns]::GetHostByAddress($global:address).HostName
  192. }
  193.  
  194. function CallOut-Names {
  195. Param($from)
  196.     $global:allNames = @()
  197.  
  198.     foreach ($remotename in $from)
  199.     {
  200.     $nextname = [System.Net.Dns]::GetHostByAddress($remotename).HostName
  201.     $nextaddress = $remotename
  202.     $global:allNames += $nextname.ToLower()+","+$nextaddress
  203.  
  204.  
  205.     }
  206.  
  207.     $lookupTable = @{}
  208.     $ScriptDirNames = $Location
  209.     $ScriptDirNames += '\NetworkNames.txt'
  210.  
  211.     $global:lineTable = @()
  212.     $lookupTable = Get-Content -raw $ScriptDirNames | ConvertFrom-StringData
  213.     $global:allNames | ForEach-Object {
  214.         $line = $_
  215.  
  216.         $lookupTable.GetEnumerator() | ForEach-Object {
  217.             if ($line -match $_.Key)
  218.             {
  219.                 $line = $line -replace $_.Key, $_.Value
  220.                 $global:lineTable += $line
  221.             }
  222.         }
  223. }
  224. }
  225.  
  226. function UpdateEntries {
  227. $External_Variables = $Location
  228. $External_Variables+= "\ExternalVariables.txt"
  229. $global:lineTable | out-file $External_Variables
  230.  
  231. }
  232.  
  233. function UpdateEntriesBlank {
  234. $External_Variables = $Location
  235. $External_Variables+= "\ExternalVariables.txt"
  236. Clear-Content $External_Variables
  237. }
  238.  
  239. function MakeToolTip
  240. {
  241. [CmdletBinding(SupportsShouldProcess = $true)]
  242. param
  243. (
  244. [Parameter(Mandatory=$true)]
  245. $Text,
  246.  
  247. [Parameter(Mandatory=$true)]
  248. $Title,
  249.  
  250. [ValidateSet(‘None’, ‘Info’, ‘Warning’, ‘Error’)]
  251. $Icon = ‘Info’,    $Timeout = 10000
  252. )
  253.  
  254. Add-Type -AssemblyName System.Windows.Forms
  255.  
  256. if ($script:balloon -eq $null)
  257. {
  258. $script:balloon = New-Object System.Windows.Forms.NotifyIcon
  259. }
  260.  
  261. $path                    = Get-Process -id $pid | Select-Object -ExpandProperty Path
  262. $balloon.Icon            = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  263. $balloon.BalloonTipIcon  = $Icon
  264. $balloon.BalloonTipText  = $Text
  265. $balloon.BalloonTipTitle = $Title
  266. $balloon.Visible         = $true
  267.  
  268. $balloon.ShowBalloonTip($Timeout)
  269. }
  270.  
  271. MakeToolTip "Testing" "VNC Connections" Info
  272. CheckThe-Lines
  273. CallOut-Names $allAddress
  274. UpdateEntries
  275. $previoustate = $statc
  276.  
  277. While($global:breaker -gt 0){
  278.  
  279. $ScriptDir = $Location
  280. $ScriptDir += "\Break.txt"
  281. $External_Variables = Get-Content -Path $ScriptDir
  282. $Variable_Checker = $External_Variables | %{$_ -match '1'}
  283. if($Variable_Checker -contains $TRUE){$global:breaker=1}else{$global:breaker=0}
  284.  
  285. $previoustate = $statc
  286.  
  287.  
  288. CheckThe-Lines
  289.  
  290. if($statc -gt $previoustate)
  291. {   $lastconnection = $global:lineTable[-1]
  292.     $message = "New Connection From $lastconnection"
  293.     CallOut-Names $allAddress
  294.     UpdateEntries
  295.     MakeToolTip $message "VNC Connections" Info
  296. }
  297.  
  298. if($statc -lt $previoustate)
  299. {
  300.     $message = "A Connection Closed"
  301.     CallOut-Names $allAddress
  302.     UpdateEntries
  303.     MakeToolTip $message "VNC Connections"
  304. }
  305.  
  306. if($statc -eq 0)
  307. {
  308.  
  309.     UpdateEntriesBlank
  310. }
  311.  
  312. Sleep 1
  313. }
  314. }
  315.  
  316. function Startup {
  317. $path = $Global:Location
  318. $path += '\NetworkNames.txt'
  319. [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
  320.  
  321. $title = 'Network Naming Setup'
  322.  
  323. if(Test-Path $path -PathType Leaf){
  324. $job1 = Start-Job $FormLib -Name "FormJob"  -ArgumentList $Global:Location
  325. $null1 = Register-ObjectEvent $job1 -EventName StateChanged -Action {
  326.     if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Completed)
  327.     {
  328.         #Write-Host -ForegroundColor Green 'Event 1 triggering'
  329.  
  330.         # This command removes the original job
  331.         $sender | Remove-Job -Force
  332.  
  333.         # These commands remove the event registration
  334.         $eventSubscriber | Unregister-Event -Force
  335.         $eventSubscriber.Action | Remove-Job -Force
  336.     }
  337. }
  338.  
  339. $job2 = Start-Job $LisLib -Name "ListenJob" -ArgumentList $Global:Location
  340. $null2 = Register-ObjectEvent $job2 -EventName StateChanged -Action {
  341.     if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Completed)
  342.     {
  343.         #Write-Host -ForegroundColor Green 'Event 2 triggering'
  344.  
  345.         # This command removes the original job
  346.         $sender | Remove-Job -Force
  347.  
  348.         # These commands remove the event registration
  349.         $eventSubscriber | Unregister-Event -Force
  350.         $eventSubscriber.Action | Remove-Job -Force
  351.     }
  352. }
  353. }
  354. else{
  355.  
  356. $nameList = switch -regex (NET.EXE VIEW) { "^\\\\(?<Name>\S+)\s+" {$matches.Name}}
  357. $nameList = $nameList.ToLower()
  358. $nameArray = @{}
  359. if ($nameList.Count -gt 1){
  360. foreach($name in $nameList)
  361. {
  362.    
  363.     $nameArray.add($name, [Microsoft.VisualBasic.Interaction]::InputBox("What is the name of $name", $title))
  364.    
  365. }}
  366.  
  367. $outArray = @()
  368. foreach ($i in $nameArray.keys)
  369. {
  370.   $outArray += ("{0} = {1}" -f $i, $nameArray[$i])
  371. }
  372. $outArray | out-file $path
  373.  
  374. Startup
  375.  
  376. }
  377. }
  378. Startup
Add Comment
Please, Sign In to add comment