I'm trying to create a backup script that unlocks an external bitlocker drive, robocopies files to it, then locks it when it's done. I am able to unlock and copy just fine, but the final step of "Lock-Bitlocker" is giving me an "access denied" error because the drive is still in use (e.g. the robocopy is still wrapping up).
Unlock-Bitlocker e: -Password (ConvertTo-SecureString "omgClearText" -AsPlainText -Force)
Robocopy "\\MyFileServer\C$\Files" "E:\" /Bunch /of /parameters
Lock-Bitlocker e:
I've tried using Wait-Job to wait for Robocopy to complete, but that doesn't work. I've considered Lock-Bitlocker E: -ForceDismount but I don't want to dismount until after all files have successfully copied over.
Is there a way to properly wait for the system to copy before locking?