views:

637

answers:

3

I am creating a suite of custom cmdlets and providers in C#. I also have a PowerShell SnapIn that takes care of registering the cmdlets and providers. I have exported a console session so that I can start PowerShell with the -PSConsoleFile parameter to automatically load my snap in.

I also want to mount a drive whenever I run PS with the snap in. Effectively, I want the following to run at the start of the PS session:

new-psdrive -name [drivename] -psprovider FileSystem -root [path to a folder on local file system]

I have tried putting the above command in a .ps1 file and starting PS with -command and the path to the .ps1 file, also specifying the -NoExit flag. The script does run, but the drive is not mapped in the subsequent session.

Is there a simple way to create a new psdrive in the snap in? I also looked into deriving from the FileSystemProvider but it is sealed. I looked into running the NewPSDriveCommand programmatically but that does not appear to be supported.

Am I missing something simple here?

Thanks!

Edit: I forgot to mention that I don't want to use the the profile file to accomplish this if possible. I want to distribute this snap in to others and I would rather they didn't have to edit their profiles to get everything to work.

A: 

You might try adding the statements to your Powershell global or environment profile. Both are located at %username%\My Documents\WindowsPowerShell. Your global profile is named profile.ps1 and your environment profile is named for each shell (Microsoft.PowerShell_profile.ps1 for the default Powershell environment).

I have a couple of new-psdrive statements in my profile.ps1 file. The downfall to this method is waiting on Powershell to connect all of those PSDrives if they are on a slow server.

Dustin Venegas
Thanks for this suggestion, but I actually wanted to avoid using profiles. Please see my edit in the original question.
shampoopy
+2  A: 

You can create a PSDrive in you snapin. There is a method that you can override, InitializeDefaultDrives, as part of your provider.

Example:

protected override Collection<PSDriveInfo> InitializeDefaultDrives()
     {
      Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();

      drives.Add(new PSDriveInfo(
       "YourDriveName",
       ProviderInfo,
       "YourDriveRoot",
       "Description of Your Drive",
       null));

      return drives;
     }

After re-reading you question and comment: You might be able to get the reference to the filesystem providerinfo object from the Microsoft.PowerShell.Core namespace... I haven't tested that yet though...

The FileSystem provider information from PowerShell is:

PS C:\scripts\PowerShell> Get-PSProvider filesystem | fl *


ImplementingType : Microsoft.PowerShell.Commands.FileSystemProvider
HelpFile         : System.Management.Automation.dll-Help.xml
Name             : FileSystem
PSSnapIn         : Microsoft.PowerShell.Core
ModuleName       : Microsoft.PowerShell.Core
Module           :
Description      :
Capabilities     : Filter, ShouldProcess
Home             : H:\
Drives           : {C, A, D, H...}
Steven Murawski
Thank you, but what base class would I be deriving from here? The drive will be created using the provider specified by ProviderInfo which is coming from the base class in your sample code. Anyone know if there is a way to get a reference to the ProviderInfo that FileSystemProvider itself uses?
shampoopy
Sorry, I misread your question. I thought you wanted to mount a PSDrive to one of your custom providers.
Steven Murawski
A: 

You can put the command in the ps1 file and add the option -scope Global.

dfowler