-
Notifications
You must be signed in to change notification settings - Fork 293
Closed
Labels
Description
Given you're accepting a $Path parameter in your function...
I think there are 4 scenarios, which are the flip sides of two conditions:
Condition One: Does it need to already exist?
Condition Two: Do you care about the provider?
- You need the $path to exist, you don't care about the provider
- You don't care if the $path exists, you don't care about the provider
- You need the $path to exist, you need it to be a specific provider
- You don't care if it exists, you need it to be a specific provider
Have I missed any scenarios?
By far the hardest of these to deal with (I think) is when the path may not exist yet, and it has to be a specific provider. For instance, if you want to take a relative or absolute path to a file and pass it to $xml.Save($path)
If the path is relative, you must convert it to a full path, because [Environment]::CurrentDirectory might as well be randomized. Otherwise, you could probably leave it alone. But assuming it might be relative, and assuming your script might be called while $pwd is in a non-filesystem folder ... what do you do?
Does this work? Is there something simpler?
$PsCmdlet.SessionState.Path.PushCurrentLocation("fallback");
$PsCmdlet.SessionState.Path.SetLocation( $PsCmdlet.SessionState.Path.CurrentFileSystemLocation );
$Path = $PsCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path);
$PsCmdlet.SessionState.Path.PopLocation("fallback");