Run command on premature script exit?

by Typeo at 2013-04-22 08:26:00

I was curious if there is a way to run a basic command if in the middle of a script, it got closed out?

For example, I have a script that starts out by mapping a couple drives, and at the end of the script, it un-maps them. If somebody where to exit the script mid way the drives would still be mapped. Is there a way to make sure this does not happen?
by mjolinor at 2013-04-22 08:39:13
You can use Try/Catch/Finally, and put your commands to unmap the drives in the Finally block.
by Typeo at 2013-04-22 09:55:39
I have never messed with Try/Catch/Finally before. Sounds like it could be very useful though, so I will def. be reading into it.

Thank you very much for the suggestion. Ill report back with the results!
by Typeo at 2013-04-22 10:18:17
I can’t seem to get it to work correctly. It seems that it is used to catch errors and then do X when that error is caught. However, an error is not generated whenever somebody force closes a running script (That I am aware of anyways).

It seems that when I exit, it stops the script and it does not get a chance to get to the Try/Catch/Finally code. I will keep looking into it however, as I am sure I am just not understanding it 100% yet.
by MasterOfTheHat at 2013-04-22 12:12:33
I was wondering if the Try/Catch/Finally would actually work in an abnormal termination, but the last sentence in the help text came to the rescue, (about_Try_Catch_Finally):
A Finally block runs even if you use CTRL+C to stop the script. A Finally
block also runs if an Exit keyword stops the script from within a Catch

So, Typeo, take a look at that help page and see if it makes more sense. The catch block will only execute when a terminating error is caught, but the finally block will always execute, regardless of whether the script executes successfully, an error was encountered, or the execution was killed by the user.
by Typeo at 2013-04-22 12:39:08
I can’t seem to wrap my head around this to get it to work. I will continue to dig into this, but in case anybody was curious, I posted my code below. I have tried to insert a Try/Catch/Finally block in a few different places, but no matter what I try I cant seem to get the mapped drive removed whenever I do a CTRL+C or just hit the X in the top right…

#Script to tranfer Luna PDF files from the projects drive to the Luna server

Write-Host 'Where is the sorce file located?'
Write-Host ''
Write-Host 'Example - Redskins_00\Scans'
Write-Host 'Example - Redskins_01\Redskin'
Write-Host ''

#Sets Location variable
$Location = Read-Host 'Enter the location path'

Write-Host ''
Write-Host 'Whats the file (PDF) name?'
Write-Host ''
Write-Host 'Example - rs1975'
Write-Host 'Example - rs1973'
Write-Host ''

#Sets Name variable
$Name = Read-Host 'Enter the file name'

#Cabines the Location and Name Variable
$File = "T:$Location$Name$Name.pdf"

#Maps the Luna Redskins drive
Net use Y: \Server\Fileshare Password /User:Domain\Username

#sets the Parent Variable, while recursing through the Luna folders to find the parent folder to help build the destination path
$Parent = Get-ChildItem -Path Y:\ -Recurse | Where-Object {$ -eq $name} | Select-Object -ExpandProperty parent

#combines the Parent and Name variable to set the destination path
$Destination = "Y:$Parent$Name"

#Checks to make sure the correct destination
if ($parent -eq $null)
Write-Host 'Error - The file destination could not be found. Please try again or contact the server Admin!'
Read-Host 'Press Enter to continue'

#Delets the mapped drive
Net use Y: /Delete

#Exits Script

Write-Host 'You are about to Copy the following file:'
Write-Host ''
Write-Host 'Source File:'
Write-Host "$file"
Write-Host ''
Write-Host 'Destination Folder:'
Write-Host "$Destination"
Write-Host ''

$Continue = Read-Host 'Press Enter to continue or enter exit to cancel'

If ($continue -eq 'exit')
#Delets the mapped drive
Net use Y: /Delete

#Exits Script

#Moves the file from the Projects folder to Luna
Copy-Item -path $File -Destination $Destination

#Delets the mapped drive
Net use Y: /Delete
by ArtB0514 at 2013-04-22 12:56:48
Ahhh, since you only need a temporary connection to the remote drive and don’t really need to have it visible outside your PowerShell script and since you want it to go away if the script exits abnormally, then you might really want to be using a PSDrive instead of mapping a drive. Check out Get-Help New-PSDrive -Full to get some instruction and examples. The PSDrive will automatically get cleaned up when PowerShell exits whether or not you’ve used a Try-Catch-Finally block.
by Typeo at 2013-04-22 13:32:17
I looked into PSDrives to begin with, but it seemed like I was unable to get it to work when mapping a network drive as I was unable to use the credentials parameter. It states:

[quote]The provider does not support the use of credentials. Perform the operation again witho
ut specifying credentials.[/quote]

Seeing as the server I am trying to connect to is not on the the a domain, I need to use the credentials parameter to complete the connection.


Did some digging around and it appears that Credentials for New-PSDrive are suported in Version 3 of Powershell, however, are not in Version 2.0.
by mjolinor at 2013-04-22 15:30:00
[quote] I have tried to insert a Try/Catch/Finally block in a few different places, but no matter what I try I cant seem to get the mapped drive removed whenever I do a CTRL+C or just hit the X in the top right… [/quote]

Try/Catch/Finally work in response to errors encountered within the script. I don’t know that there’s going to be anything you’ll be able to do within the script that will still run if the entire process is terminated externally.
by Typeo at 2013-04-25 06:09:22
Just wanted to update and close the thread. I was not able to figure out how to get a script to run when somebody force closed the program, however, I did go ahead and update my PowerShell to v3 and used the PSdrive with credentials. This allowed me to map a temporary PSDrive that then gets removed on the scripts exit (premature or not).