11<#
22. SYNOPSIS
3-
3+ Monitor when USB is inserted and removed.
44. DESCRIPTION
5-
5+ The script ran in task sechduler on system startup. It monitors a USB letter and Name
6+ If USB removal event is triggered it will shut down the system.
67. NOTES
8+ All events are logged
79. LINK
8-
10+ https://github.com/PowerShellCrack/USBStatus/edit/master/Detect-USBStatus.ps1
911#>
1012# #*===============================================
1113# #* VARIABLE DECLARATION
1214# #*===============================================
15+ [string ]$TaskName = " Monitor USB Boot Key - System Startup"
16+ [string ]$USBLetter = " I:"
17+ [string ]$USBName = " BOOTKEY"
18+ # #*===============================================
19+ # #* Do not modify section below
1320$t = ' [DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
1421add-type - name win - member $t - namespace native
1522[native.win ]::ShowWindow(([System.Diagnostics.Process ]::GetCurrentProcess() | Get-Process ).MainWindowHandle, 0 )
23+
1624# # Variables: Permissions/Accounts
1725[Security.Principal.WindowsIdentity ]$CurrentProcessToken = [Security.Principal.WindowsIdentity ]::GetCurrent()
1826[Security.Principal.SecurityIdentifier ]$CurrentProcessSID = $CurrentProcessToken.User
@@ -25,20 +33,24 @@ add-type -name win -member $t -namespace native
2533[boolean ]$IsServiceAccount = [boolean ]($CurrentProcessToken.Groups -contains [Security.Principal.SecurityIdentifier ]' S-1-5-6' )
2634[boolean ]$IsProcessUserInteractive = [Environment ]::UserInteractive
2735[string ]$LocalSystemNTAccount = (New-Object - TypeName ' System.Security.Principal.SecurityIdentifier' - ArgumentList ([Security.Principal.WellKnownSidType ]::' LocalSystemSid' , $null )).Translate([Security.Principal.NTAccount ]).Value
36+
2837# Check if script is running in session zero
2938If ($IsLocalSystemAccount -or $IsLocalServiceAccount -or $IsNetworkServiceAccount -or $IsServiceAccount ) { $SessionZero = $true } Else { $SessionZero = $false }
3039
31-
32- [string ]$ScriptName = " Monitor USB Boot Key"
33- [string ]$ScriptVersion = " 1.0"
40+ # #*===============================================
41+ # #* PATH VARIABLE DECLARATION
42+ # #*===============================================
43+ # # Variables: Script Name and Script Paths
44+ [string ]$scriptPath = $MyInvocation.MyCommand.Definition
45+ [string ]$scriptDirectory = Split-Path - Path $scriptPath - Parent
3446
3547$RunningDate = Get-Date - Format MMddyyyy
3648If ($SessionZero ) {
3749 $FinalLogFileName = ($ScriptName.Trim (" " ) + " (SYSTEM)-" + $RunningDate )
3850} Else {
3951 $FinalLogFileName = ($ScriptName.Trim (" " ) + " (" + $env: USERNAME + " )-" + $RunningDate )
4052}
41- [string ]$Logfile = " E:\Data\Processors \Logs\$FinalLogFileName .log"
53+ [string ]$Logfile = " $scriptDirectory \Logs\$FinalLogFileName .log"
4254
4355# #*===============================================
4456# #* FUNCTIONS
@@ -166,9 +178,9 @@ Function Get-ScheduledTasks{
166178# #*===============================================
167179# #* MAIN
168180# #*===============================================
169- $RunningTasks = Get-ScheduledTask - TaskName ' Monitor USB Boot Key - System Startup '
181+ $RunningTasks = Get-ScheduledTask - TaskName $TaskName
170182If (! $SessionZero -and $RunningTasks.State -eq " Running" ){
171- Stop-ScheduledTask - TaskName ' Monitor USB Boot Key - System Startup '
183+ Stop-ScheduledTask - TaskName $TaskName
172184 taskkill / IM powershell.exe / FI " USERNAME eq SYSTEM"
173185}
174186Unregister-Event - SourceIdentifier volumeChange - ErrorAction SilentlyContinue
@@ -193,32 +205,37 @@ do{
193205 Write-Log ((get-date - format s) + " Drive name = " + $driveLetter ) - writehost
194206 Write-Log ((get-date - format s) + " Drive label = " + $driveLabel ) - writehost
195207 # Execute process if drive matches specified condition(s)
196- if ($driveLetter -eq ' I: ' -and $driveLabel -eq ' BOOTKEY ' ){
208+ if ( ( $driveLetter -eq $USBLetter ) -and ( $driveLabel -eq $USBName ) ){
197209 Write-Log ((get-date - format s) + " Starting task in 3 seconds..." ) - writehost
198210 # Stop-Computer -computerName $env:COMPUTERNAME -force
199211 # start-process "Z:\sync.bat"
200212 }
201- } ElseIf ($eventType -eq 3 ){
213+ }
214+ ElseIf ($eventType -eq 3 ){
202215 $driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
203- if ($driveLetter -eq ' I: ' ){
216+ if ($driveLetter -eq $USBLetter ){
204217 If ($SessionZero ) {
205218 Write-Log ((get-date - format s) + " USB Key removal event detected, rebooting system..." ) - writehost
206219 Stop-Computer - computerName $env: COMPUTERNAME - Force
207220 } Else {
208221 Write-Log ((get-date - format s) + " USB Key removal event detected, sending message..." ) - writehost
209222 $result = Show-PopUp - Message USB Key ($driveLetter ) was removed`n`n System shutdown will be triggered in 30 seconds, Continue ? - Title USB Key removal - TimeOut 30 - ButtonSet " OC" - IconType " Exclamation"
210- If ($result -eq 1 ){ # Accepted
223+
224+ If ($result -eq 1 ){ # Accepted
211225 Write-Log ((get-date - format s) + " User accepted, Shutting down system..." ) - writehost
212226 Stop-Computer - computerName $env: COMPUTERNAME - force
213- } ElseIf ($result -eq 2 ){ # Cancelled
227+ }
228+ ElseIf ($result -eq 2 ){ # Cancelled
214229 Write-Log ((get-date - format s) + " User cancelled system shutdown..." ) - writehost
215- } Else { # Let message continue
230+ }
231+ Else { # Let message continue
216232 Write-Log ((get-date - format s) + " Countdown ended, Shutting down system..." ) - writehost
217233 Stop-Computer - computerName $env: COMPUTERNAME - force
218234 }
219235 }
220236 }
221237 }
222238 Remove-Event - SourceIdentifier volumeChange
223- } while (1 -eq 1 ) # Loop until next event
239+ }
240+ while (1 -eq 1 ) # Loop until next event
224241Unregister-Event - SourceIdentifier volumeChange
0 commit comments