views:

137

answers:

1

In my Powershell provider, which is a business-logic layer over an SQL database, the Powershell drives equate to database connection settings. This collection of connection settings is persisted to an encrypted XML file on the local computer. An administrator may add these PS drives on a computer, supplying SQL Server credentials, that another Powershell user may use when writing scripts. When the administrator runs New-PSDrive, I add a drive to the collection and persist it to the XML. When they Remove-PSDrive, I remove the drive from the persistent XML. This works fine. However, when the Powershell host is exiting, it calls RemoveDrive() for each existing drive. At this point I do not want to remove the drives since I need them to persist on the computer.

How do I determine if RemoveDrive() is being called from a user manually executing Remove-PSDrive, or the host exiting?

Also, I have created custom drive info parameters which work well, but I also wrote a custom cmdlet which is used to add and remove drives. How can I create (and remove) psdrives from my cmdlet?

A: 

You might want to use the InitializeDefaultDrives override for this. This does mean that you will need to have that persistent store of connection strings so that you can create all the appropriate drives when the provider initializes. You should be able to override the Stop virtual method to save this info.

Keith Hill
@keith: I am overriding InitializeDefaultDrives(), and in that function I load my collection of persisted drives from disk.The issue is that I need to allow the scripter to execute a Remove-PSDrive (which calls back into RemoveDrive()) so they can remove drives. At that point I need to remove that drive and de-persist it from my xml file, so to speak. However, when powershell exits it is also calling RemoveDrive() for each loaded drive. In this case I do not want to de-persist them. In RemoveDrive(), how do I know if the scripter is executing it or Powershell is exiting?
Jack Straw
Does the CmdletProvider.Stopping property vary depending on whether the user or PowerShell removes the drive?
Keith Hill
Keith is probably on the right track here - you could also set a flag by overriding Stop on the provider too - if the flag is set, powershell is shutting down but I'm not sure which is called first: provider.stop or remove-psdrive...
x0n
CmdletProvider.Stopping indicates that a pipeline process is stopping, not that the provider is stopping. I tried overriding Stop() but it gets executed after RemoveDrive(). Still searching...
Jack Straw