Unmount vhdx on error in script

I want to check to see if a vhdx is still mounted and unmount it if there’s an error in the install script. I thought I could just put code in the ‘Catch’ section at the end of the ‘Deploy-Applications.ps1’ script. Apparently not.
Is there anything I can do to make that work?

Test-VHD is probably your best bet https://technet.microsoft.com/en-us/library/hh848580.aspx

I suspect you need to add -ErrorAction ‘Stop’ to ensure you get a terminal error to get ‘Catch’ to work.

I’m not trying to test anything with the vhd, I’m just trying to get some code to run if the script crashes for some reason. I can figure out what to do with the vhd etc.
I think -ErrorAction ‘Stop’ is the default for the Deploy-Application.ps1 script anyway. It does error out when something catestrophic happens.
How do I add code to run when the script exits suddenly?

Then your original idea should work UNLESS the error happens before Resolve-Error is declared.

If that is the case, look at the code I proposed in issue 163

All I’m doing (I’m sorry, the vhdx was a bit of a red herring) is using Execute-Process with a non-existent executable to trigger the fail. Then I want to insert some code that runs when the fail happens.
I’d be happy if Write-Host ‘Something bad just happened’ ran :wink: I can fill in the rest later.

I think I’m going to need more info like where are you placing this Execute-Process line in Deploy-Applications.ps1

Maybe the Execute-Process line itself, too.

I’m just placing something like Execute-Process -Path ‘C:\temp\something.exe’ in the Installation section of the script. It’s not very important except that I’m forcing an error to occur, something.exe doesn’t exist.
Then I put some code in the Catch at the end of the script thinking that it would execute when the function failed.

I tried and got this:

<code>…
[07-28-2016 12:11:36.410] [Initialization] [Execute-Process] :: [C:\temp\something.exe] is a valid fully qualified path, continue.
[07-28-2016 12:11:36.445] [Initialization] [Execute-Process] :: Function failed, setting exit code to [60002].
Error Record:

Message : File [C:\temp\something.exe] not found.
InnerException :

FullyQualifiedErrorId : File [C:\temp\something.exe] not found.
ScriptStackTrace : at Execute-Process&lt;Process&gt;, C:&amp;lt;path&gt;\PSADT_v3.6.9_Beta\AppDeployToolkit\AppDeployToolkitMain.ps1: line 2766
at &lt;ScriptBlock&gt;, C:\Users\UserName\AppData\Local\Temp\e3523fab-7a02-476f-8eee-0f3ddae37418.ps1: line 2
at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 1

PositionMessage : At C:&amp;lt;path&gt;\PSADT_v3.6.9_Beta\AppDeployToolkit\AppDeployToolkitMain.ps1:2766 char:6
+ Throw &quot;File [$Path] not found.&quot;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[07-28-2016 12:11:36.460] [Initialization] [Exit-Script] :: Deploy-Application_v1r1 Installation completed with exit code [60002].
[07-28-2016 12:11:36.475] [Initialization] [Test-PowerPoint] :: Check if PowerPoint is in either fullscreen slideshow mode or presentation mode…
[07-28-2016 12:11:36.486] [Initialization] [Test-PowerPoint] :: PowerPoint application is not running.
[07-28-2016 12:11:36.493] [Initialization] [Test-PowerPoint] :: PowerPoint is running in fullscreen mode [False].
[07-28-2016 12:11:36.504] [Initialization] [Show-BalloonTip] :: Display balloon tip notification with message [Installation failed.].
…</code>

Try to give it a REAL file to look at. Make it a 0 byte file.