-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathSamCommandBase.cs
More file actions
90 lines (81 loc) · 2.35 KB
/
SamCommandBase.cs
File metadata and controls
90 lines (81 loc) · 2.35 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
using System.ComponentModel;
using System.Management.Automation;
using System.Net;
using DSInternals.Common.Interop;
using DSInternals.SAM;
namespace DSInternals.PowerShell.Commands;
public abstract class SamCommandBase : PSCmdletEx, IDisposable
{
private const string DefaultServer = "localhost";
private string server;
protected SamServer SamServer
{
get;
private set;
}
#region Parameters
[Parameter(
HelpMessage = "Specify the user account credentials to use to perform this task. The default credentials are the credentials of the currently logged on user.",
Mandatory = false,
ValueFromPipeline = false
)]
[ValidateNotNullOrEmpty]
[Credential]
public PSCredential Credential
{
get;
set;
}
[Parameter(
HelpMessage = "Specify the name of a SAM server.",
Mandatory = false,
ValueFromPipeline = false
)]
[Alias("ComputerName", "Computer")]
[PSDefaultValue(Value = DefaultServer)]
[ValidateNotNullOrEmpty]
public string Server
{
get
{
return this.server ?? DefaultServer;
}
set
{
this.server = value;
}
}
#endregion Parameters
#region Cmdlet Overrides
protected override void BeginProcessing()
{
/* Connect to the specified SAM server: */
WriteDebug($"Connecting to SAM server {this.Server}.");
try
{
NetworkCredential? netCred = this.Credential?.GetNetworkCredential();
this.SamServer = new(this.Server, SamServerAccessMask.LookupDomain | SamServerAccessMask.EnumerateDomains, netCred, useNamedPipes: true);
}
catch (Win32Exception ex)
{
ErrorCategory category = ((Win32ErrorCode)ex.NativeErrorCode).ToPSCategory();
ErrorRecord error = new(ex, "WinAPIErrorConnect", category, this.Server);
// Terminate on this error:
this.ThrowTerminatingError(error);
}
}
#endregion Cmdlet Overrides
public void Dispose()
{
this.Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing && this.SamServer != null)
{
this.SamServer.Dispose();
this.SamServer = null;
}
}
}