Reputation: 113
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.
Upvotes: 1
Views: 2050
Reputation: 1940
Are you talking about creating a : function so that you can switch to that drive as you can with the file system drives like c: ?
I have the following code at the end of my NewDrive method.
// create the <drive>: alias
string func = string.Format("function {0}: {{ set-location {0}: }}", drive.Name);
this.InvokeCommand.InvokeScript(
func
, false
, System.Management.Automation.Runspaces.PipelineResultTypes.None
, null
, null);
And then a matching remove call in the RemoveDrive method that removes the matching function.
string func = string.Format("function {0}: {{ remove-item function:\ {0}: }}"
, drive.Name);
this.InvokeCommand.InvokeScript(
func
, false
, System.Management.Automation.Runspaces.PipelineResultTypes.None
, null
, null);
Upvotes: 0
Reputation: 38834
You can put the command in the ps1 file and add the option -scope Global.
Upvotes: 0
Reputation: 11255
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...}
Upvotes: 2
Reputation: 759
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.
Upvotes: 0