• PowerShell – Testers Needed

    Home » Forums » Developers, developers, developers » DevOps Lounge » PowerShell – Testers Needed

    • This topic has 11 replies, 5 voices, and was last updated 2 years ago.
    Author
    Topic
    #2547047

    Hey Y’all,

    Have you experienced that dreaded “Unable to Remove Device” error message when trying to eject a USB attached drive? I’ve developed a PowerShell program which should tell you what application/process is blocking the removal and offer to get rid of it for you.

    Of course, since writing this I have been unable to fully test it as I can’t get any of my devices to fail the removal request!

    I’ve set the program to kick off via a scheduled task that detects ID 225 in the System Event Log.

    Here’s how you set it up:

    1. Download the PowerShell (Test-EjectionFailure.zip) from here.
    2. Unzip it and place it where ever you want.
    3. Setup a Scheduled Task to run the script as follows:
      Set to run as Admin (may not be necessary but who knows).
      Set the Action:
      Task-Action
      Set Program/Script: “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”
      Set Arguments: “d:\yourpathhere\Test-EjectionFailure.ps1”
      Event-Trigger
      Set Trigger: as shown above.
    4. Save Task.

    Now just wait for a USB device to fail to eject and follow the insturctions in the program, if it pops up! Let me know how things turn out.

    Worst case is the program will stop the wrong process (read the message carefully before allowing it to do that!) requiring a reboot to get things back to normal.

    TIA!

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!
    Computer Specs

    1 user thanked author for this post.
    Viewing 7 reply threads
    Author
    Replies
    • #2547052

      Here’s some sample messages:
      Couldn’t find any 225 events.
      USB-Ejection-NoEvents

      Found a 225 event and shows associated program.
      USB-Ejection-WithEvents

      Note: you have to read carefully to see that it was Samsung Magician that caused the problem. Of course I got this message while developing the program not in actual practice.

      Note: The current version of the program offers to stop the process for you so you don’t have to type the commands shown, but they are still shown for your edification!

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      1 user thanked author for this post.
    • #2547081

      I’ll try again tomorrow morning, because this is something that would be of interest to me. Unfortunately, although I’m able to make the “Unable to remove device” message appear on demand, I’ve not been able to get your utility to pop up anything meaningful as yet. Something happens, a brief blue box flashes up but it disappears immediately

      I’ve tried the Program/Script and Add Arguments fields both with, and without, inverted commas, but to no avail

      I expect I’ve done something dumb, so, as I said, I’ll try again tomorrow (Europe time)

      • #2547210

        NNNM,

        That blue box is powershell starting or attempting to start. That means the Scheduled task is firing on the event as designed. Unfortunately, I didn’t mention in the instructions about setting powershell to run scripts, disabled by default.

        Run PowerShell as admin and then type this command:

        Set-ExecutionPolicy RemoteSigned -Force

        Answer Yes to any prompts but you shouldn’t get any and this only needs to be done once.

        You also need to kill the “Mark of the Internet” using File Explorer right-clicking on the .ps1 file and selecting properties then Check the Unblock check box.

        Sorry, I keep forgetting that not everyone uses powershell…LOL!

        May the Forces of good computing be with you!

        RG

        PowerShell & VBA Rule!
        Computer Specs

        1 user thanked author for this post.
        • #2547283

          Hi RG, thanks for the explicit instructions. Once I’d followed them I was able to get the task to work successfully

          I’ve attached the results. I didn’t actually try selecting Yes, as “System” seemed a fairly non-specific application to terminate

          If it’s any help, the steps I took to force the Rejection message were:

          1. Attach one of my USB backup drives
          2. Open and mount one of the files on it in TrueCrypt
          3. Try and eject the device using “Safely Remove Hardware and Eject Media”

          I have the policy for the device set to “Quick Removal”, but I always use “Safely Remove…”  because I’m a cautious soul. If there’s any other info you’d like just let me now

    • #2547093

      Strange, that “Unable to remove device” issue hasn’t happened for years in Windows 7, 8.1 or 10 here over several computers and various flash drives/ external HDD’s.
      I have encountered it on other ‘alien client’ devices which require a restart as log-outs make no bloody difference and it is an inconvenience when it does happen and usually with external HDD’s but, not flashdrives..some sort of external hook

      Windows - commercial by definition and now function...
    • #2547429

      NNNM,

      Thanks!

      Did you by any chance use Process Explorer to determine what Process ID 4 was since the message was very vague?

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #2547574

      it is an inconvenience when it does happen and usually with external HDD’s but, not flashdrives..some sort of external hook

      I am Win10/Pro 22H2. Yes, it always happens with me whenever I finish the native Backup App at Settings>Update & Security>Backup>Backup and Restore (Windows 7) on a WD Easy Store External HDD or when I plug in a WD My Passport Ultra External HDD to do a ‘Passport’ backup. It’s something peculiar to a WD external HDD. I don’t have this problems with my Seagate external HDD.

      I go to Task Manager>Processes and look for WD Backup or WD Drive Service in the list, right-click on it, and select End Task. About 3 seconds later I can get the ‘Safely Remove’ pop up.

    • #2547579

      Did you by any chance use Process Explorer to determine what Process ID 4 was

      I didn’t, but I’ve just run the test again together with Process Explorer, and it doesn’t really seem to give any extra useful information

      As fas as I can tell (from its description in Resource Monitor) Process Id 4 is “NT Kernel & System”, so I’m guessing it’s one that shouldn’t be shut down…

    • #2547728

      NNNM,

      Thanks for checking!

      RG

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #2553932

      Hey Y’all,

      I just had this popup today after I did my bi-weekly backup of my Tiny11 Laptop installation.
      Trap-Ejection-Error
      I selected Yes and got this one:
      Trap-Ejection-Could-NOT-Stop-Process
      It seems that this Process ID 4 is truly a problem.

      I’ll do some more research on this and see if there is a way I can filter processes that can’t be shutdown and give an appropriate message.

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 7 reply threads
    Reply To: PowerShell – Testers Needed

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: