execute powershell script with popupmessage in Windows10 Shutdown

117 Views Asked by At

I have created a powershell script that asks me when it is executed whether a certain USB stick (name and label is defined) was correctly unmounted. But only if it's still plugged in and it has the defined driveletter).

The reason is encryption software that has to unmount the USB stick itself before unmounting it (encrypt and release the drive). If this does not happen and the PC shuts down, the USB stick must be re-encrypted when it is started again, which takes about 30 minutes and is not desired.

With the script, the customer should now be informed via popup that the corresponding USB stick is still inserted and that the unmount command may still have to be executed in the encryption software (does not work automatically). If he has just forgotten to remove the stick, he can continue to shut down with the popup with "yes - just forget". with "no" the shutdown should be canceled.

So far so good, the script works, but I can't get the script to run when the PC shuts down.

What I've already tried:

  • powercfg.exe / hibernate off in the command line
  • Entered the script as Powershell in gpedit.msc and set it to "execute first"
  • Script execution and message output activated in gpedit.msc
  • the path of the file is: C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown\umount.ps1

What is happening: Windows shutdown screen appears, but no message. The shutdown process hangs.

My Script:

$driveLabel = 'USBSTICK'
$driveLetter = (Get-Volume -FileSystemLabel $driveLabel).DriveLetter
$driveletterShouldBe = 'F'
write-host (get-date -format s) " Drive name = " $driveLetter
write-host (get-date -format s) " Drive label = " $driveLabel
# Execute process if drive matches specified condition(s)
if ($driveLetter -eq $driveletterShouldBe){
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $result = [System.Windows.Forms.MessageBox]::Show('Ist der USB Stick absteckbar?' , "Info" , 4)
    if ($result -eq 'Yes') {
        $Eject = New-Object -comObject Shell.Application    
        $Eject.NameSpace(17).ParseName($driveLetter+“:”).InvokeVerb(“Eject”)
    }else{
        exit
    }
} else {
    #just for testing in console
    write-host (get-date -format s) " Drive letter = " $driveLetter
    write-host (get-date -format s) " Drive letter should be = " $driveletterShouldBe
    write-host (get-date -format s) " Drive label = " $driveLabel
    sleep(5)
}
´´´

Hope someone can help me
Thank you in advance
Best
Cypherfax
0

There are 0 best solutions below