Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Last active November 29, 2017 21:24
Show Gist options
  • Save tallpeak/80d00c78305f7eb4b81f44b3302a17f6 to your computer and use it in GitHub Desktop.
Save tallpeak/80d00c78305f7eb4b81f44b3302a17f6 to your computer and use it in GitHub Desktop.
Watch bitlocker encrypting a drive
// duplicating this PowerShell one-liner in F#:
// (Get-BitLockerVolume C:).EncryptionPercentage[1]
// Or perhaps this slightly longer version:
// while ($true) { Get-BitLockerVolume C: | %{ echo $_.EncryptionPercentage } |
// Tee-Object -FilePath c:\temp\bitlockerstatuslog.txt -Append ; [Console]::Out.Flush() ; sleep 1 }
#r "System.Management"
open System
open System.Management
open System.Linq
let strComputer = "."
let scope = ManagementScope(sprintf @"\\%s\root\CIMV2\Security\MicrosoftVolumeEncryption" strComputer)
let qstr = "SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter='C:'"
let qry : ObjectQuery = SelectQuery(qstr) :> ObjectQuery
let srch = new ManagementObjectSearcher( scope, qry)
let managementObject = seq { for m in srch.Get() -> m } |> Seq.head :?> ManagementObject
// http://csharp.wekeepcoding.com/article/11025277/System.UnauthorizedAccessException+while+encrypting+drive+in+system+programmatically+using+Encrypt+method+of+Win32_EncryptableVolume+in+UWP
printfn "n\tEncryptionPercentage"
for loop = 1 to 20000 do
let o6 : obj[] = Array.create 6 (box 0)
let numdecimals = 4 // this is the max; encryptionPercentage comes back 0 if 5 decimals are requested
o6.[5] <- box numdecimals
let n = managementObject.InvokeMethod("GetConversionStatus", o6)
let encryptionPercentage : uint32 = unbox o6.[1]
let epfloat = float encryptionPercentage * (10.0 ** (-1.0 * float numdecimals))
printfn "%2d\t%.*f" loop numdecimals epfloat
System.Threading.Thread.Sleep(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment