-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathEnable-BitLocker.ps1
71 lines (58 loc) · 2.57 KB
/
Enable-BitLocker.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$Creds = Get-Credential
$Computers = @(
'computer1', 'computer2'
)
# Log file
$Date = (Get-Date).ToString('yyyyMMdd-HHmm')
$LogFolder = New-Item -ItemType Directory "C:\Logs" -Force
$Log = New-Item -ItemType File "$LogFolder\Enable-BitLocker-$Date.log" -Force
foreach ($Computer in $Computers) {
Add-Content $Log "$Computer..."
if (Test-Connection $Computer -Quiet -Count 3) {
$Return = Invoke-Command -ComputerName $Computer -Credential $Creds -ScriptBlock {
# Delete any current BitLocker pin
try {
$Result = ((manage-bde -protectors -delete C: | Select-String -Pattern "(?<=(ERROR:)).*").ToString()).TrimStart()
$Return = 'Deleted old protectors.'
} catch {
$Return += $Error
}
$Return += "`n"
# Add new Pin and TPM security
try {
$Result = ((manage-bde -protectors -add C: -tp newpin | Select-String -Pattern "(?<=(ERROR:)).*").ToString()).TrimStart()
$Return += 'Added new protectors.'
} catch {
$Return += $Error
}
$Return += "`n"
# Get the BitLocker's status after changes
$Result = ((manage-bde -status | Select-String -Pattern "(?<=(Protection Status:)).*").ToString()).Replace('Protection Status:', '').TrimStart()
if (!$Result) {
$Return += 'ERROR: Unable to capture BitLocker status.'
} elseif ($Result -eq 'Protection Off') {
# Enable protection
try {
$Result = ((manage-bde -protectors -enable C: | Select-String -Pattern "(?<=(ERROR:)).*").ToString()).TrimStart()
$Return += 'Enabled protectors.'
} catch {
$Return += $Error
}
$Return += "`n"
# Get the BitLocker's status after changes
$Result = ((manage-bde -status | Select-String -Pattern "(?<=(Protection Status:)).*").ToString()).Replace('Protection Status:', '').TrimStart()
if ($Result -eq 'Protection Off') {
$Return += 'ERROR: Unable to enable protectors.'
} elseif ($Result -eq 'Protection On') {
$Return += $Result
}
} elseif ($Result -eq 'Protection On') {
$Return += $Result
}
Return $Return
}
Add-Content $Log $Return
} else {
Add-Content $Log "ERROR: Failed to connect."
}
}