-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathGetPSScriptFileInfo.cs
More file actions
90 lines (74 loc) · 3.27 KB
/
GetPSScriptFileInfo.cs
File metadata and controls
90 lines (74 loc) · 3.27 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
90
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// Retrieve the contents of a .ps1 file
/// </summary>
[Cmdlet(VerbsCommon.Get, "PSScriptFileInfo")]
[OutputType(typeof(PSScriptFileInfo))]
public sealed class GetPSScriptFileInfo : PSCmdlet
{
#region Parameters
/// <summary>
/// The path to the .ps1 file to retrieve.
/// </summary>
[Parameter(Position = 0, Mandatory = true, HelpMessage = "Path (including file name) to the script file (.ps1 file) to retrieve and view.")]
[ValidateNotNullOrEmpty]
public string Path { get; set; }
#endregion
#region Methods
protected override void EndProcessing()
{
if (!Path.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("The script file pathname must end with a .ps1 file extension. Example: C:/Users/john/x/MyScript.ps1"),
"InvalidPath",
ErrorCategory.InvalidArgument,
this));
}
var resolvedPaths = GetResolvedProviderPathFromPSPath(Path, out ProviderInfo provider);
if (resolvedPaths.Count != 1)
{
ThrowTerminatingError(new ErrorRecord(
new PSArgumentException("Error: Could not resolve provided Path argument into a single path."),
"InvalidPathArgumentError",
ErrorCategory.InvalidArgument,
this));
}
var resolvedPath = resolvedPaths[0];
bool isValidScript = PSScriptFileInfo.TryTestPSScriptFileInfo(
scriptFileInfoPath: resolvedPath,
parsedScript: out PSScriptFileInfo psScriptFileInfo,
errors: out ErrorRecord[] errors,
out string[] debugMsgs);
if (!isValidScript)
{
string fileName = System.IO.Path.GetFileName(resolvedPath);
var exMessage = $"Error: '{fileName}' script file is invalid. The script file must include Version, Guid, Description and Author properties.";
foreach (ErrorRecord error in errors)
{
exMessage += Environment.NewLine + error.Exception.Message;
}
ThrowTerminatingError(new ErrorRecord(
new PSArgumentException(exMessage),
"InvalidPSScriptFile",
ErrorCategory.InvalidArgument,
this));
}
PSObject psScriptFileInfoWithName = new PSObject(psScriptFileInfo);
string Name = System.IO.Path.GetFileNameWithoutExtension(resolvedPath);
psScriptFileInfoWithName.Properties.Add(new PSNoteProperty(nameof(Name), Name));
foreach (string msg in debugMsgs)
{
WriteDebug(msg);
}
WriteObject(psScriptFileInfoWithName);
}
#endregion
}
}