Guest User

Untitled

a guest
May 22nd, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.42 KB | None | 0 0
  1. $job1 = Start-Job { MakeForm } -Name "FormJob" -InitializationScript $FormLib;
  2. $null1 = Register-ObjectEvent $job1 -EventName StateChanged -Action {
  3.     if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Completed)
  4.     {
  5.         Write-Host -ForegroundColor Green 'Event 1 triggering';
  6.  
  7.         # This command removes the original job
  8.         $sender | Remove-Job -Force;
  9.  
  10.         # These commands remove the event registration
  11.         $eventSubscriber | Unregister-Event -Force;
  12.         $eventSubscriber.Action | Remove-Job -Force;
  13.     }
  14. }
  15.  
  16. $job2 = Start-Job { ListenIn } -Name "ListenJob" -InitializationScript $LisLib;
  17. $null2 = Register-ObjectEvent $job2 -EventName StateChanged -Action {
  18.     if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Failed)
  19.     {
  20.         Write-Host -ForegroundColor Green 'Event 2 triggering';
  21.  
  22.         # This command removes the original job
  23.         $sender | Remove-Job -Force;
  24.  
  25.         # These commands remove the event registration
  26.         $eventSubscriber | Unregister-Event -Force;
  27.         $eventSubscriber.Action | Remove-Job -Force;
  28.     }
  29. }
  30.  
  31. Write-Verbose -Verbose 'Before:';
  32.  
  33. Get-Job | Out-Host;
  34.  
  35. $FormLib = {
  36. [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms");  
  37. [System.Windows.Forms.Application]::EnableVisualStyles();  
  38.  
  39. $global:break = @('1');
  40. $break | out-file -filepath C:\Users\VS-Print-Server\Desktop\Break.txt;
  41.  
  42. $Form                            = New-Object System.Windows.Forms.Form;
  43. $Timer                           = New-Object System.Windows.Forms.Timer;
  44. $ConListView                     = New-Object System.Windows.Forms.ListView;
  45. $Close                           = New-Object System.Windows.Forms.Button;
  46. $SlideUp                         = New-Object System.Windows.Forms.Button;
  47. $SlideDown                       = New-Object System.Windows.Forms.Button;
  48. $TheLabel                        = New-Object System.Windows.Forms.Label;
  49.  
  50. function MakeForm {
  51.  
  52.  
  53. $TotalTime = -1 #in seconds
  54. $Timer.Start();
  55.  
  56. $Timer_Tick={
  57.  
  58. $ConListView.Items.Clear();
  59. $ScriptDir = Split-Path $MyInvocation.MyCommand.Path;
  60. $ScriptDir += "C:\Users\VS-Print-Server\Desktop\ExternalVariables.txt";
  61. $External_Variables = Get-Content -Path $ScriptDir;
  62. foreach ($string in $External_Variables)
  63.     {
  64.         #$ConItem = [System.Net.Dns]::GetHostByAddress($string).HostName
  65.         $ConItem = $string;
  66.         $ConListView.Items.Add($string);
  67.  
  68.     }      
  69. }
  70.  
  71. $Form.ClientSize                     = '300,130';
  72. $Form.text                           = "Form";
  73. $Form.BackColor                      = "#000000";
  74. $Form.TopMost                        = $True;
  75. $Form.StartPosition                  = 2;
  76. $Form.ControlBox                     = $True;
  77. $Form.Text                           = "VNC Connections";
  78. $Form.FormBorderStyle                = 'None';
  79. $Form.StartPosition                  = [System.Windows.Forms.FormStartPosition]::Manual;
  80. $Form.Location                       = New-Object System.Drawing.Point(750,0)
  81.  
  82. $Close.Location                      = New-Object System.Drawing.Point(210,40);
  83. $Close.Text                          ='Close';
  84. $Close.BackColor                     = "#FFC20E";
  85. $Close.Add_Click({
  86.     $Form.Close();
  87.     $break=0;
  88.     $break | out-file -filepath C:\Users\VS-Print-Server\Desktop\Break.txt;
  89. })
  90.  
  91. $SlideUp.Location                    = New-Object System.Drawing.Point(120,100);
  92. $SlideUp.Visible                     = $True;
  93. $SlideUp.Text                        ='Slide Up';
  94. $SlideUp.BackColor                   = "#FFC20E";
  95. $SlideUp.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,-100); $SlideUp.Visible = $false; $SlideDown.Visible =$true;})
  96.  
  97. $SlideDown.Location                  = New-Object System.Drawing.Point(120,100);
  98. $SlideDown.Visible                   = $False;
  99. $SlideDown.Text                      ='Slide Down';
  100. $SlideDown.BackColor                 = "#FFC20E";
  101. $SlideDown.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,0); $SlideDown.Visible = $false; $SlideUp.Visible =$true;})
  102.  
  103. $ConListView.View                    = 'Details';
  104. $ConListView.Width                   = 180;
  105. $ConListView.Height                  = 80;
  106. $ConListView.HeaderStyle             = ColumnHeaderStyle.NotClickable;
  107. $ConListView.text                    = "listView";
  108. $ConListView.BackColor               = "#FFC20E";
  109. $ConListView.ForeColor               = "#FFFFFF";
  110. $ConListView.Dock                    = System.Windows.Forms.DockStyle.Top;
  111. $ConListView.Columns.Add("Connections", -2);
  112. $ConListView.FullRowSelect           = True;
  113. $ConListView.Anchor                  = 'top';
  114. $ConListView.Location                = New-Object System.Drawing.Point(10,10);
  115. $ConListView.Font                    = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Regular)
  116.  
  117.  
  118. $Form.controls.AddRange(@($ConListView, $Close, $SlideUp, $SlideDown));
  119. $Form.Add_Shown({$Form.Activate()});
  120.  
  121.  
  122. $Timer.add_Tick($Timer_Tick)
  123. $Form.ShowDialog();
  124.     }
  125. }
  126.  
  127. $LisLib = {
  128.  
  129. Import-Module NetTCPIP;
  130.  
  131. $global:allNames = @("VS-PRINT-SERVER");
  132.  
  133. $global:breaker=1;
  134.  
  135. function CheckThe-Lines{
  136.    
  137.     if((Get-NetTCPConnection -LocalPort 5900 | Measure-Object).Count -gt 1)
  138.     {
  139.         $global:stat=(Get-NetTCPConnection -LocalPort 5900 -State Established);
  140.         $global:statc = ($global:stat | Measure-Object).Count
  141.         $global:address = $global:stat[0].RemoteAddress;
  142.         $global:allAddress = $global:stat.RemoteAddress;
  143.        
  144.        
  145.     }
  146.     else {$global:statc=0};
  147. }
  148.  
  149. function defineName {
  150. $global:name = [System.Net.Dns]::GetHostByAddress($global:address).HostName
  151. }
  152.  
  153. function CallOut-Names {
  154. Param($from)
  155.     $global:allNames = @("VS-PRINT-SERVER");
  156.     foreach ($remotename in $from)
  157.     {
  158.     $nextname = [System.Net.Dns]::GetHostByAddress($remotename).HostName
  159.     $global:allNames+=$nextname.ToLower();
  160.     }
  161. }
  162.  
  163. function UpdateEntries {
  164. #Write-Host $statc "|" $previoustate "|" $allNames;
  165. $global:allNames | out-file -filepath C:\Users\VS-Print-Server\Desktop\ExternalVariables.txt
  166. }
  167.  
  168. function MakeToolTip {
  169. Param($TheMessage)
  170. Add-Type -AssemblyName System.Windows.Forms | Out-Null;
  171. $path = (Get-Process -id $pid).Path;
  172. $objBalloon = New-Object System.Windows.Forms.NotifyIcon;
  173. $objBalloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path);
  174.  
  175. # You can use the value Info, Warning, Error
  176. $objBalloon.BalloonTipIcon = "Info";
  177.  
  178. # Put what you want to say here for the Start of the process
  179. $objBalloon.BalloonTipTitle = "VNC Connections";
  180. $objBalloon.BalloonTipText = $TheMessage;
  181. $objBalloon.Visible = $True;
  182. $objBalloon.ShowBalloonTip(10);
  183. $objBalloon.Dispose();
  184. }
  185.  
  186. CheckThe-Lines
  187. #defineName
  188. CallOut-Names $allAddress
  189. UpdateEntries
  190.  
  191. While($global:breaker -gt 0){
  192.  
  193. #$ScriptDir = Split-Path $MyInvocation.MyCommand.Path;
  194. $ScriptDir = "C:\Users\VS-Print-Server\Desktop\Break.txt";
  195. $External_Variables = Get-Content -Path $ScriptDir;
  196. $Variable_Checker= $External_Variables | %{$_ -match '1'}
  197. if($Variable_Checker -contains $true){$global:breaker=1;}else{$global:breaker=0;}
  198.  
  199. $previoustate = $statc;
  200. $lastconnection = $allNames[-1]
  201.  
  202. CheckThe-Lines
  203.  
  204. if($statc -gt $previoustate)
  205. {
  206.     MakeToolTip "New Connection From $lastconnection"
  207.     CallOut-Names $allAddress
  208.     UpdateEntries
  209. }
  210.  
  211. if($statc -lt $previoustate)
  212. {
  213.     CallOut-Names $allAddress
  214.     MakeToolTip "A Connection Closed"
  215.     UpdateEntries
  216. }
  217.  
  218. #UpdateEntries
  219. Sleep 1;
  220. }
  221. }
Add Comment
Please, Sign In to add comment