Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $job1 = Start-Job { MakeForm } -Name "FormJob" -InitializationScript $FormLib;
- $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 { ListenIn } -Name "ListenJob" -InitializationScript $LisLib;
- $null2 = Register-ObjectEvent $job2 -EventName StateChanged -Action {
- if ($eventArgs.JobStateInfo.State -eq [System.Management.Automation.JobState]::Failed)
- {
- 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;
- }
- }
- Write-Verbose -Verbose 'Before:';
- Get-Job | Out-Host;
- $FormLib = {
- [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms");
- [System.Windows.Forms.Application]::EnableVisualStyles();
- $global:break = @('1');
- $break | out-file -filepath C:\Users\VS-Print-Server\Desktop\Break.txt;
- $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;
- $SlideUp = New-Object System.Windows.Forms.Button;
- $SlideDown = New-Object System.Windows.Forms.Button;
- $TheLabel = New-Object System.Windows.Forms.Label;
- function MakeForm {
- $TotalTime = -1 #in seconds
- $Timer.Start();
- $Timer_Tick={
- $ConListView.Items.Clear();
- $ScriptDir = Split-Path $MyInvocation.MyCommand.Path;
- $ScriptDir += "C:\Users\VS-Print-Server\Desktop\ExternalVariables.txt";
- $External_Variables = Get-Content -Path $ScriptDir;
- foreach ($string in $External_Variables)
- {
- #$ConItem = [System.Net.Dns]::GetHostByAddress($string).HostName
- $ConItem = $string;
- $ConListView.Items.Add($string);
- }
- }
- $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,40);
- $Close.Text ='Close';
- $Close.BackColor = "#FFC20E";
- $Close.Add_Click({
- $Form.Close();
- $break=0;
- $break | out-file -filepath C:\Users\VS-Print-Server\Desktop\Break.txt;
- })
- $SlideUp.Location = New-Object System.Drawing.Point(120,100);
- $SlideUp.Visible = $True;
- $SlideUp.Text ='Slide Up';
- $SlideUp.BackColor = "#FFC20E";
- $SlideUp.Add_Click({$Form.Location = New-Object System.Drawing.Point(750,-100); $SlideUp.Visible = $false; $SlideDown.Visible =$true;})
- $SlideDown.Location = New-Object System.Drawing.Point(120,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.Columns.Add("Connections", -2);
- $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)
- $Form.controls.AddRange(@($ConListView, $Close, $SlideUp, $SlideDown));
- $Form.Add_Shown({$Form.Activate()});
- $Timer.add_Tick($Timer_Tick)
- $Form.ShowDialog();
- }
- }
- $LisLib = {
- Import-Module NetTCPIP;
- $global:allNames = @("VS-PRINT-SERVER");
- $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 = @("VS-PRINT-SERVER");
- foreach ($remotename in $from)
- {
- $nextname = [System.Net.Dns]::GetHostByAddress($remotename).HostName
- $global:allNames+=$nextname.ToLower();
- }
- }
- function UpdateEntries {
- #Write-Host $statc "|" $previoustate "|" $allNames;
- $global:allNames | out-file -filepath C:\Users\VS-Print-Server\Desktop\ExternalVariables.txt
- }
- function MakeToolTip {
- Param($TheMessage)
- Add-Type -AssemblyName System.Windows.Forms | Out-Null;
- $path = (Get-Process -id $pid).Path;
- $objBalloon = New-Object System.Windows.Forms.NotifyIcon;
- $objBalloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path);
- # You can use the value Info, Warning, Error
- $objBalloon.BalloonTipIcon = "Info";
- # Put what you want to say here for the Start of the process
- $objBalloon.BalloonTipTitle = "VNC Connections";
- $objBalloon.BalloonTipText = $TheMessage;
- $objBalloon.Visible = $True;
- $objBalloon.ShowBalloonTip(10);
- $objBalloon.Dispose();
- }
- CheckThe-Lines
- #defineName
- CallOut-Names $allAddress
- UpdateEntries
- While($global:breaker -gt 0){
- #$ScriptDir = Split-Path $MyInvocation.MyCommand.Path;
- $ScriptDir = "C:\Users\VS-Print-Server\Desktop\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;
- $lastconnection = $allNames[-1]
- CheckThe-Lines
- if($statc -gt $previoustate)
- {
- MakeToolTip "New Connection From $lastconnection"
- CallOut-Names $allAddress
- UpdateEntries
- }
- if($statc -lt $previoustate)
- {
- CallOut-Names $allAddress
- MakeToolTip "A Connection Closed"
- UpdateEntries
- }
- #UpdateEntries
- Sleep 1;
- }
- }
Add Comment
Please, Sign In to add comment