-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathResetPSResourceRepository.cs
More file actions
70 lines (57 loc) · 2.34 KB
/
ResetPSResourceRepository.cs
File metadata and controls
70 lines (57 loc) · 2.34 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Management.Automation;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// The Reset-PSResourceRepository cmdlet resets the repository store by creating a new PSRepositories.xml file.
/// This is useful when the repository store becomes corrupted.
/// It will create a new repository store with only the PSGallery repository registered.
/// </summary>
[Cmdlet(VerbsCommon.Reset,
"PSResourceRepository",
SupportsShouldProcess = true,
ConfirmImpact = ConfirmImpact.High)]
[OutputType(typeof(PSRepositoryInfo))]
public sealed class ResetPSResourceRepository : PSCmdlet
{
#region Parameters
/// <summary>
/// When specified, displays the PSGallery repository that was registered after reset
/// </summary>
[Parameter]
public SwitchParameter PassThru { get; set; }
#endregion
#region Methods
protected override void ProcessRecord()
{
string repositoryStorePath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSResourceGet",
"PSResourceRepository.xml");
WriteVerbose($"Resetting repository store at: {repositoryStorePath}");
if (!ShouldProcess(repositoryStorePath, "Reset repository store and create new PSRepositories.xml file with PSGallery registered"))
{
return;
}
PSRepositoryInfo psGalleryRepo = RepositorySettings.Reset(this, out string errorMsg);
if (!string.IsNullOrEmpty(errorMsg))
{
WriteError(new ErrorRecord(
new PSInvalidOperationException(errorMsg),
"ErrorResettingRepositoryStore",
ErrorCategory.InvalidOperation,
this));
return;
}
WriteVerbose("Repository store reset successfully. PSGallery has been registered.");
if (PassThru && psGalleryRepo != null)
{
WriteObject(psGalleryRepo);
}
}
#endregion
}
}