Last active
November 8, 2024 22:50
-
-
Save AnandPilania/4119df60f5e23aac03628b918072a37e to your computer and use it in GitHub Desktop.
Task Scheduler BitLocker Unlock Script - PowerShell script automates the setup of a Task Scheduler task to unlock a BitLocker-encrypted drive upon user login.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Task Scheduler BitLocker Unlock Script | |
| This PowerShell script automates the setup of a Task Scheduler task to unlock a BitLocker-encrypted D: drive upon user login. | |
| ## Features: | |
| - Generates a Task Scheduler XML definition and a corresponding PowerShell script to unlock the BitLocker-encrypted D: drive. | |
| - Prompts the user for input, allowing them to choose whether to import the task into Task Scheduler. | |
| - If the user presses Enter without providing input, the script defaults to 'Y' for task import. | |
| - Provides an option to cancel the task creation if the user chooses not to import it. | |
| ## Usage: | |
| 1. Run the script to generate the necessary XML and PowerShell script files. | |
| 2. The script prompts the user with a "Do you want to import the task to Task Scheduler? (Y/N)" question. | |
| 3. If the user enters 'Y' or 'y', the script imports the task into Task Scheduler, unlocking the BitLocker-encrypted D: drive on login. | |
| 4. If the user enters 'N' or any other input, the task creation is canceled. | |
| **Note:** | |
| - Change `__DRIVE__` & `__PASSWORD_FOR_D_DRIVE__` before using it. | |
| - Ensure that the script is executed with elevated privileges (Run as Administrator) for Task Scheduler manipulation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $authorDomainUsername = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name | |
| $userId = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value | |
| $currentDirectory = $PSScriptRoot | |
| # Create Task XML for TaskScheduler | |
| $xmlContent = @" | |
| <?xml version="1.0" encoding="UTF-16"?> | |
| <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> | |
| <RegistrationInfo> | |
| <Date>$(Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffffff")</Date> | |
| <Author>$authorDomainUsername</Author> | |
| <URI>\unlock</URI> | |
| </RegistrationInfo> | |
| <Triggers> | |
| <LogonTrigger> | |
| <Enabled>true</Enabled> | |
| <UserId>$authorDomainUsername</UserId> | |
| </LogonTrigger> | |
| </Triggers> | |
| <Principals> | |
| <Principal id="Author"> | |
| <UserId>$userId</UserId> | |
| <LogonType>InteractiveToken</LogonType> | |
| <RunLevel>HighestAvailable</RunLevel> | |
| </Principal> | |
| </Principals> | |
| <Settings> | |
| <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> | |
| <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> | |
| <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> | |
| <AllowHardTerminate>true</AllowHardTerminate> | |
| <StartWhenAvailable>false</StartWhenAvailable> | |
| <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> | |
| <IdleSettings> | |
| <StopOnIdleEnd>true</StopOnIdleEnd> | |
| <RestartOnIdle>false</RestartOnIdle> | |
| </IdleSettings> | |
| <AllowStartOnDemand>true</AllowStartOnDemand> | |
| <Enabled>true</Enabled> | |
| <Hidden>false</Hidden> | |
| <RunOnlyIfIdle>false</RunOnlyIfIdle> | |
| <WakeToRun>false</WakeToRun> | |
| <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> | |
| <Priority>7</Priority> | |
| </Settings> | |
| <Actions Context="Author"> | |
| <Exec> | |
| <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command> | |
| <Arguments>$currentDirectory\unlock.ps1</Arguments> | |
| </Exec> | |
| </Actions> | |
| </Task> | |
| "@ | |
| $xmlFilePath = Join-Path $currentDirectory "unlock.xml" | |
| $xmlContent | Out-File -FilePath $xmlFilePath -Encoding UTF8 | |
| # Create PS script | |
| # Change __DRIVE__ [ex: D:] & __PASSWORD_FOR_D_DRIVE__ [ex: abc@123] | |
| $unlockScriptContent = @" | |
| `$pswd = ConvertTo-SecureString "__PASSWORD_FOR_D_DRIVE__" -AsPlainText -Force | |
| Unlock-BitLocker -MountPoint "__DRIVE__" -Password `$pswd | |
| "@ | |
| $unlockScriptPath = Join-Path $currentDirectory "unlock.ps1" | |
| $unlockScriptContent | Out-File -FilePath $unlockScriptPath -Encoding UTF8 | |
| # The fancy thing | |
| $userInput = Read-Host "Do you want to import the task to Task Scheduler? (Y/n)" | |
| $userInput = if ($userInput -eq '') {'Y'} else {$userInput} | |
| if ($userInput -eq 'Y' -or $userInput -eq 'y') { | |
| Register-ScheduledTask -Xml (Get-Content $xmlFilePath | Out-String) -TaskName "unlock" -Force | |
| Write-Host "Task has been imported to Task Scheduler." | |
| } else { | |
| Write-Host "Task creation canceled. Exiting..." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment