Skip to content

Commit

Permalink
Avoid downloading application to implement eject-media (chef#1529)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Bradbury <[email protected]>
  • Loading branch information
MBradbury authored Sep 21, 2023
1 parent e3da652 commit da8c149
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packer_templates/scripts/windows/eject-media.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ trap {
#
# eject removable volume media.

Write-Host 'Downloaing EjectVolumeMedia...'
$ejectVolumeMediaExeUrl = 'https://github.com/rgl/EjectVolumeMedia/releases/download/v1.0.0/EjectVolumeMedia.exe'
$ejectVolumeMediaExeHash = 'f7863394085e1b3c5aa999808b012fba577b4a027804ea292abf7962e5467ba0'
$ejectVolumeMediaExe = "$env:TEMP\EjectVolumeMedia.exe"
Invoke-WebRequest $ejectVolumeMediaExeUrl -OutFile $ejectVolumeMediaExe
$ejectVolumeMediaExeActualHash = (Get-FileHash $ejectVolumeMediaExe -Algorithm SHA256).Hash
if ($ejectVolumeMediaExeActualHash -ne $ejectVolumeMediaExeHash) {
throw "the $ejectVolumeMediaExeUrl file hash $ejectVolumeMediaExeActualHash does not match the expected $ejectVolumeMediaExeHash"
}
$volList = Get-Volume | Where-Object {$_.DriveType -ne 'Fixed' -and $_.DriveLetter}

ForEach ($vol in $volList) {
$volLetter = $vol.DriveLetter
Write-Host "Ejecting drive ${volLetter}:"

try {
$Eject = New-Object -comObject Shell.Application

# Namespace 17 represents ssfDRIVES
# See: https://learn.microsoft.com/en-us/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants

Get-Volume | Where-Object {$_.DriveType -ne 'Fixed' -and $_.DriveLetter} | ForEach-Object {
&$ejectVolumeMediaExe $_.DriveLetter
$Eject.NameSpace(17).ParseName("${volLetter}:").InvokeVerb("Eject")
} finally {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Eject) | Out-Null
}
}

0 comments on commit da8c149

Please sign in to comment.