Repository navigation
Expand file tree
/
Copy pathPSPathUtility.cs
More file actions
89 lines (81 loc) · 3.83 KB
/
Copy pathPSPathUtility.cs
File metadata and controls
89 lines (81 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
namespace NuGet.PowerShell.Commands
{
public static class PSPathUtility
{
/// <summary>
/// Translate a PSPath into a System.IO.* friendly Win32 path.
/// Does not resolve/glob wildcards.
/// </summary>
/// <param name="session">The SessionState to use.</param>
/// <param name="psPath">The PowerShell PSPath to translate which may reference PSDrives or have provider-qualified paths which are syntactically invalid for .NET APIs.</param>
/// <param name="path">The translated PSPath in a format understandable to .NET APIs.</param>
/// <param name="exists">Returns null if not tested, or a bool representing path existence.</param>
/// <param name="errorMessage">If translation failed, contains the reason.</param>
/// <returns>True if successfully translated, false if not.</returns>
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#", Justification = "Following BCL TryParse pattern.")]
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "3#", Justification = "Following BCL TryParse pattern.")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "ps", Justification = "ps is a common powershell prefix")]
public static bool TryTranslatePSPath(SessionState session, string psPath, out string path, out bool? exists, out string errorMessage)
{
if (String.IsNullOrEmpty(psPath))
{
throw new ArgumentException(
String.Format(
CultureInfo.CurrentCulture,
CommonResources.Argument_Cannot_Be_Null_Or_Empty, "psPath"));
}
bool succeeded = false;
path = null;
exists = null;
errorMessage = null;
// session is null during unit tests
if (session == null)
{
return false;
}
if (!session.Path.IsValid(psPath))
{
errorMessage = String.Format(
CultureInfo.CurrentCulture,
Resources.Cmdlet_InvalidPathSyntax, psPath);
}
else
{
try
{
// we do not glob wildcards (literalpath.)
exists = session.InvokeProvider.Item.Exists(psPath, force: false, literalPath: true);
ProviderInfo provider;
PSDriveInfo drive;
// translate pspath, not trying to glob.
path = session.Path.GetUnresolvedProviderPathFromPSPath(psPath, out provider, out drive);
// ensure path is on the filesystem (not registry, certificate, variable etc.)
if (provider.ImplementingType != typeof(FileSystemProvider))
{
errorMessage = Resources.Cmdlet_InvalidProvider;
}
else
{
succeeded = true;
}
}
catch (ProviderNotFoundException)
{
errorMessage = Resources.Cmdlet_InvalidProvider;
}
catch (DriveNotFoundException ex)
{
errorMessage = String.Format(
CultureInfo.CurrentCulture,
Resources.Cmdlet_InvalidPSDrive, ex.ItemName);
}
}
return succeeded;
}
}
}