Advertisement
FlyFar

patch.ps1

Jul 30th, 2023
1,575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 9.61 KB | Cybersecurity | 0 0
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3. Add-Type -AssemblyName PresentationFramework
  4. $running = $true
  5. $Host.UI.RawUI.BackgroundColor = "DarkBlue"
  6. function displayProcess
  7. {
  8.     $saveFolder = New-Object System.Windows.Forms.saveFiledialog -Property @{
  9.         InitialDirectory = [Environment]::GetFolderPath('Desktop')
  10.         Filter = "Log Files|*.Log|Text File|*.txt| All Files| *.*"
  11.         Title = 'Save As'
  12.         ShowHelp = $true
  13.        
  14.      }
  15.    
  16.     $main_form = New-Object System.Windows.Forms.Form
  17.     $main_form.Text = 'Processor Monitor'
  18.     $main_form.Width = 850
  19.     $main_form.Height = 600
  20.     $main_form.AutoSize = $true
  21.     $main_form.StartPosition = 'CenterScreen'
  22.     $main_form.BackColor = '#9e9c96'
  23.  
  24.  
  25.     $btnScheduled = New-Object System.Windows.Forms.Button
  26.     $btnScheduled.Enabled = $true
  27.     $btnScheduled.Location = New-Object System.Drawing.Size(250,40)
  28.     $btnScheduled.Size = New-Object System.Drawing.Size(400, 25)
  29.     $btnScheduled.Text = 'Show Scheduled Tasks'
  30.     $btnScheduled.Add_Click(
  31.     {
  32.         $lstTasks.Items.Clear()
  33.         $lstTasks.Items.Add("Scheduled Tasks:")
  34.         $lstTasks.Items.Add("")
  35.         foreach($task in Get-ScheduledTask | Select-Object -Property TaskName | Sort-Object -Property TaskName)
  36.         {
  37.             $lstTasks.Items.Add($task.TaskName)
  38.         }
  39.         $lstTasks.Enabled = $true
  40.         $btnDelete.Enabled = $true
  41.         $btnRemove.Enabled = $true
  42.     })
  43.  
  44.     $lstProcess = New-Object System.Windows.Forms.ListBox
  45.     $lstProcess.Location = New-Object System.Drawing.Point(30,70)
  46.     $lstProcess.Width = 400
  47.     $lstProcess.Height = 400
  48.     $lstProcess.BackColor = "#000000"
  49.     $lstProcess.ForeColor = "#ffffff"
  50.     $lstProcess.Items.Add("Top 20 Running Processes:")
  51.     $lstProcess.Items.Add("")
  52.     foreach($process in Get-Process| Sort-Object -Property CPU -Descending | Select-Object -Property Name -First 20)
  53.     {
  54.         $lstProcess.Items.Add($process.Name)
  55.     }
  56.  
  57.     $lstProcess.Add_Click(
  58.     {
  59.  
  60.         if(($lstProcess.SelectedItem.ToString() -ne "") -and ($lstProcess.SelectedItem.ToString() -ne "Top 20 Running Processes:"))
  61.         {
  62.             Write-Host $lstProcess.SelectedItem.ToString()
  63.             $btnStop.Enabled = $true
  64.         }
  65.         else
  66.         {
  67.             $btnStop.Enabled = $false
  68.         }
  69.     })
  70.  
  71.  
  72.     $lstTasks = New-Object System.Windows.Forms.ListBox
  73.     $lstTasks.Location = New-Object System.Drawing.Point(450,70)
  74.     $lstTasks.Width = 400
  75.     $lstTasks.Height = 400
  76.     $lstTasks.Enabled = $false
  77.      
  78.  
  79.     $lstTasks.Add_Click(
  80.     {
  81.         if($lstTasks.SelectedItem.ToString() -ne "Scheduled Tasks:" -and $lstProcess.SelectedItem.ToString() -ne "")
  82.         {
  83.             $btnDelete.Enabled = $true
  84.             $btnOpen.Enabled = $true
  85.         }
  86.     })
  87.  
  88.  
  89.     $btnStop = New-Object System.Windows.Forms.Button
  90.     $btnStop.Enabled = $false
  91.     $btnStop.Location = New-Object System.Drawing.Size(40,500)
  92.     $btnStop.Size = New-Object System.Drawing.Size(120,25)
  93.     $btnStop.Text = 'End Process'
  94.     $btnStop.Add_Click(
  95.     {
  96.         $procName = $lstProcess.SelectedItem.ToString()
  97.         $msgBoxInput =  [System.Windows.MessageBox]::Show('Are you sure that you want to remove the process ' + $procName + "?",'End Process','YesNoCancel','Question')
  98.         switch ($msgBoxInput)
  99.         {
  100.             'yes'
  101.         {
  102.            
  103.             Stop-Process -Name $procName -Force -Confirm:$false
  104.             foreach($process in Get-Process| Select-Object -Property Name -First 10 | Sort-Object -Property CPU -Descending)
  105.             {
  106.                 $lstProcess.Items.Add($process.TaskName)
  107.             }
  108.         }
  109.         'no'
  110.         {
  111.  
  112.         }
  113.         'cancel'
  114.         {
  115.  
  116.         }
  117.        }
  118.     })
  119.  
  120.     $btnRefresh = New-Object System.Windows.Forms.Button
  121.     $btnRefresh.Enabled = $true
  122.     $btnRefresh.Location = New-Object System.Drawing.Size(170,500)
  123.     $btnRefresh.Size = New-Object System.Drawing.Size(120,25)
  124.     $btnRefresh.Text = 'Refresh'
  125.     $btnRefresh.Add_click({
  126.         $lstProcess.Items.Clear()
  127.         $lstProcess.Items.Add("Top 20 Running Processes:")
  128.         $lstProcess.Items.Add("")
  129.         foreach($process in Get-Process| Sort-Object -Property CPU -Descending | Select-Object -Property Name -First 20)
  130.         {
  131.             $lstProcess.Items.Add($process.Name)
  132.         }
  133.     })
  134.  
  135.  
  136.     $btnSave = New-Object System.Windows.Forms.Button
  137.     $btnSave.Location = New-Object System.Drawing.Size(300,500)
  138.     $btnSave.Size = New-Object System.Drawing.Size(120,25)
  139.     $btnSave.Text = 'Save'
  140.  
  141.     $btnSave.Add_Click(
  142.     {
  143.         $saveFolder.ShowDialog()
  144.        
  145.  
  146.  
  147.  
  148.         $CPUPercent = @{
  149.             Name = ‘CPUPercent’
  150.             Expression =
  151.             {
  152.                 $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
  153.                 [Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
  154.             }
  155.         }
  156.  
  157.         do{
  158.  
  159.             Get-Process |
  160.             Select-Object -Property Name, CPU, $CPUPercent, Description |
  161.             Sort-Object -Property CPUPercent -Descending |
  162.             where-object {$_.CPUPercent -gt 5} |
  163.             out-file -filepath $saveFolder.FileName -encoding ASCII -width 70 -Append
  164.  
  165.             $i++
  166.             Start-Sleep -s 10
  167.  
  168.         } while ($i -lt 6)
  169.  
  170.         [System.Media.SystemSounds]::Beep.Play()
  171.  
  172.         $msgBoxInput =  [System.Windows.MessageBox]::Show(“Now wasn’t that fun.. Let’s See Whats in the file. :), "Processes Logged",'OK','Information')
  173.  
  174.         Invoke-Item $saveFolder.FileName
  175.     })
  176.  
  177.     $btnDelete = New-Object System.Windows.Forms.Button
  178.     $btnDelete.Enabled = $false
  179.     $btnDelete.Location = New-Object System.Drawing.Size(450,500)
  180.     $btnDelete.Size = New-Object System.Drawing.Size(120,25)
  181.     $btnDelete.Text = 'Remove Task'
  182.     $btnDelete.Add_Click(
  183.     {
  184.        $taskName = $lstTasks.SelectedItem.toString()
  185.        $msgBoxInput =  [System.Windows.MessageBox]::Show('Are you sure that you want to remove the task' + $taskName + "? `r`n (You cannot recover the task once removed)",'Remove Task','YesNoCancel','Warning')
  186.        switch ($msgBoxInput)
  187.        {
  188.         'yes'
  189.         {
  190.            
  191.             Stop-ScheduledTask -TaskName $taskName
  192.             Disable-ScheduledTask -TaskName $taskName
  193.             Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
  194.             $lstTasks.Items.Clear()
  195.             $lstTasks.Items.Add("Scheduled Tasks:")
  196.             $lstTasks.Items.Add("")
  197.             foreach($task in Get-ScheduledTask | Sort-Object -Property TaskName)
  198.             {
  199.                 $lstTasks.Items.Add($task.TaskName)
  200.             }
  201.         }
  202.         'no'
  203.         {
  204.  
  205.         }
  206.         'cancel'
  207.         {
  208.  
  209.         }
  210.        }
  211.     })
  212.  
  213.     $btnRemove = New-Object System.Windows.Forms.Button
  214.     $btnRemove.Enabled = $false
  215.     $btnRemove.Location = New-Object System.Drawing.Size(680,500)
  216.     $btnRemove.Size = New-Object System.Drawing.Size(150,25)
  217.     $btnRemove.Text = 'Task Was Not Removed?'
  218.     $btnRemove.Add_Click(
  219.     {
  220.         $msgBoxInput =  [System.Windows.MessageBox]::Show("Delete Task Manually and try again", "Task Was not removed",'OK','Warning')
  221.         $btnDelete.Enabled = $false
  222.         Invoke-Item C:\Windows\System32\Tasks
  223.    
  224.     })
  225.  
  226.     $main_form.Controls.Add($lstProcess)
  227.     $main_form.Controls.Add($lstTasks)
  228.     $main_form.Controls.Add($btnDelete)
  229.     $main_form.Controls.Add($btnStop)
  230.     $main_form.Controls.Add($btnRemove)
  231.     $main_form.Controls.Add($btnScheduled)
  232.     $main_form.Controls.Add($btnSave)
  233.     $main_form.Controls.Add($btnRefresh)
  234.     $main_form.ShowDialog()
  235. }
  236.  
  237. Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
  238. $cpu_threshold = 85
  239. $sleep_interval = 5
  240. $hit = 0
  241. $iloop = 0
  242. While($isRunning -ne $false)
  243. {
  244.     $cpu = (gwmi -class Win32_Processor).LoadPercentage
  245.     $CPUPercent = @{
  246.         Name = ‘CPUPercent’
  247.         Expression =
  248.         {
  249.             $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
  250.             [Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
  251.         }
  252.     }
  253.     clear-host
  254.     write-host “CPU utilization is currently at $cpu%
  255.    
  256.      
  257.     If($cpu -ge $cpu_threshold)
  258.     {
  259.         $hit = $hit+1
  260.     }
  261.     if($iloop -ge 5)
  262.     {
  263.         $hit = 0
  264.         $iloop = 0
  265.     }
  266.     start-sleep $sleep_interval
  267.     if($hit -eq 3)
  268.     {
  269.         $msgBoxInput =  [System.Windows.MessageBox]::Show("CPU Utilization is more than 85`% `r`n Would you like to view the top processes”,'CPU is over threshold level','YesNoCancel','Warning')
  270.        switch  ($msgBoxInput)
  271.        {
  272.  
  273.            'Yes'
  274.            {
  275.                displayProcess
  276.                
  277.            }
  278.            'No'
  279.            {
  280.                
  281.            }
  282.            'Cancel'
  283.            {
  284.              
  285.            }
  286.        }
  287.        
  288.        #Send-MailMessage –From CryptoMonito@mail.com –To 201477488@student.uj.ac.za –Subject “CPU Utilization is more than 85`%” –Body “CPU Utilization is more than $cpu_threshold`%” –SmtpServer smtpserver.domain.com
  289.  
  290.        $hit = 0
  291.        $loop = 0
  292.    }
  293.    else
  294.    {
  295.         If($cpu -ge $cpu_threshold)
  296.         {
  297.             $Host.UI.RawUI.BackgroundColor = "DarkRed"
  298.             Write-Host "Intense Running Processes:"
  299.             Get-Process | Sort-Object -Property CPUPercent -Descending | Select-Object -Property Name, CPU, $CPUPercent, Description -First 20
  300.  
  301.         }else
  302.         {  
  303.             write-host “CPU utilization is below threshold level”
  304.             $Host.UI.RawUI.BackgroundColor = "DarkBlue"
  305.         }
  306.    }
  307.     $iloop = $iloop + 1
  308. }
  309.  
  310.  
  311.  
  312.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement