From fb13952daf3e2dd4750d2b5e0e8ca393ce64ce64 Mon Sep 17 00:00:00 2001 From: Matthew Bradbury Date: Wed, 20 Sep 2023 17:25:52 +0100 Subject: [PATCH] Avoid downloading application to implement eject-media Signed-off-by: Matthew Bradbury --- .../scripts/windows/eject-media.ps1 | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packer_templates/scripts/windows/eject-media.ps1 b/packer_templates/scripts/windows/eject-media.ps1 index 9bea3a073..59e87ec02 100644 --- a/packer_templates/scripts/windows/eject-media.ps1 +++ b/packer_templates/scripts/windows/eject-media.ps1 @@ -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 + } }