@@ -9,53 +9,79 @@ namespace PnP.PowerShell.Commands.Base
9
9
/// </summary>
10
10
public class BasePSCmdlet : PSCmdlet
11
11
{
12
+ /// <summary>
13
+ /// Generate a new correlation id for each cmdlet execution. This is used to correlate log entries in the PnP PowerShell log stream.
14
+ /// </summary>
15
+ internal Guid ? CorrelationId { get ; } = Guid . NewGuid ( ) ;
16
+
17
+ #region Cmdlet execution
18
+
19
+ /// <summary>
20
+ /// Triggered when the cmdlet is started. This is the place to do any initialization work.
21
+ /// </summary>
12
22
protected override void BeginProcessing ( )
13
23
{
14
- Framework . Diagnostics . Log . Debug ( "PnPPowerShell" , $ "Executing { MyInvocation . MyCommand . Name } ") ;
24
+ LogDebug ( "Cmdlet execution started ") ;
15
25
base . BeginProcessing ( ) ;
16
- PnP . Framework . Diagnostics . Log . Info ( "PnP.PowerShell" , $ "Executing { this . MyInvocation . InvocationName } ") ;
17
- if ( MyInvocation . MyCommand . Name . ToLower ( ) != MyInvocation . InvocationName . ToLower ( ) )
26
+
27
+ CheckForDeprecationAttributes ( ) ;
28
+ }
29
+
30
+ /// <summary>
31
+ /// Executes the cmdlet. This is the place to do the actual work of the cmdlet.
32
+ /// </summary>
33
+ protected virtual void ExecuteCmdlet ( )
34
+ { }
35
+
36
+ /// <summary>
37
+ /// Triggered for the execution of the cmdlet. Use ExecuteCmdlet() to do the actual work of the cmdlet.
38
+ /// </summary>
39
+ protected override void ProcessRecord ( )
40
+ {
41
+ try
18
42
{
19
- var attribute = Attribute . GetCustomAttribute ( this . GetType ( ) , typeof ( WriteAliasWarningAttribute ) ) ;
20
- if ( attribute != null )
43
+ ExecuteCmdlet ( ) ;
44
+ }
45
+ catch ( Model . Graph . GraphException gex )
46
+ {
47
+ var errorMessage = gex . Error . Message ;
48
+
49
+ if ( gex . Error . Code == "Authorization_RequestDenied" )
21
50
{
22
- var warningAttribute = attribute as WriteAliasWarningAttribute ;
23
- if ( ! string . IsNullOrEmpty ( warningAttribute ? . DeprecationMessage ) )
51
+ if ( ! string . IsNullOrEmpty ( gex . AccessToken ) )
24
52
{
25
- WriteWarning ( warningAttribute . DeprecationMessage ) ;
53
+ TokenHandler . EnsureRequiredPermissionsAvailableInAccessTokenAudience ( GetType ( ) , gex . AccessToken ) ;
26
54
}
27
55
}
56
+ if ( string . IsNullOrWhiteSpace ( errorMessage ) && gex . HttpResponse != null && gex . HttpResponse . StatusCode == System . Net . HttpStatusCode . Forbidden )
57
+ {
58
+ errorMessage = "Access denied. Check for the required permissions." ;
59
+ }
60
+ throw new PSInvalidOperationException ( errorMessage ) ;
28
61
}
29
- // if (PnPConnection.Current == null)
30
- // {
31
- // if (Settings.Current.LastUserTenant != null)
32
- // {
33
- // var clientid = PnPConnection.GetCacheClientId(Settings.Current.LastUserTenant);
34
- // if (clientid != null)
35
- // {
36
- // var cancellationTokenSource = new CancellationTokenSource();
37
- // PnPConnection.Current = PnPConnection.CreateWithInteractiveLogin(new Uri(Settings.Current.LastUserTenant.ToLower()), clientid, null, Framework.AzureEnvironment.Production, cancellationTokenSource, false, null, false, false, Host);
38
- // }
39
-
40
- // }
41
- // }
42
62
}
43
63
64
+ /// <summary>
65
+ /// Triggered when the cmdlet is done executing. This is the place to do any cleanup or finalization work.
66
+ /// </summary>
44
67
protected override void EndProcessing ( )
45
68
{
46
69
base . EndProcessing ( ) ;
70
+ LogDebug ( "Cmdlet execution done" ) ;
47
71
}
48
72
49
73
/// <summary>
50
- /// Checks if a parameter with the provided name has been provided in the execution command
74
+ /// Triggered when the cmdlet is stopped
51
75
/// </summary>
52
- /// <param name="parameterName">Name of the parameter to validate if it has been provided in the execution command</param>
53
- /// <returns>True if a parameter with the provided name is present, false if it is not</returns>
54
- public bool ParameterSpecified ( string parameterName )
76
+ protected override void StopProcessing ( )
55
77
{
56
- return MyInvocation . BoundParameters . ContainsKey ( parameterName ) ;
78
+ base . StopProcessing ( ) ;
57
79
}
58
80
81
+ #endregion
82
+
83
+ #region Helper methods
84
+
59
85
protected string ErrorActionSetting
60
86
{
61
87
get
@@ -67,42 +93,84 @@ protected string ErrorActionSetting
67
93
}
68
94
}
69
95
70
- protected virtual void ExecuteCmdlet ( )
71
- { }
72
-
73
- protected override void ProcessRecord ( )
96
+ /// <summary>
97
+ /// Checks if deprecation attribute is present on the cmdlet and if so, writes a warning message to the console to notify the user to change their script to use the new cmdlet name.
98
+ /// </summary>
99
+ private void CheckForDeprecationAttributes ( )
74
100
{
75
- try
76
- {
77
- ExecuteCmdlet ( ) ;
78
- }
79
- catch ( Model . Graph . GraphException gex )
101
+ if ( MyInvocation . MyCommand . Name . ToLower ( ) != MyInvocation . InvocationName . ToLower ( ) )
80
102
{
81
- var errorMessage = gex . Error . Message ;
82
-
83
- if ( gex . Error . Code == "Authorization_RequestDenied" )
103
+ var attribute = Attribute . GetCustomAttribute ( GetType ( ) , typeof ( WriteAliasWarningAttribute ) ) ;
104
+ if ( attribute != null )
84
105
{
85
- if ( ! string . IsNullOrEmpty ( gex . AccessToken ) )
106
+ var warningAttribute = attribute as WriteAliasWarningAttribute ;
107
+ if ( ! string . IsNullOrEmpty ( warningAttribute ? . DeprecationMessage ) )
86
108
{
87
- TokenHandler . EnsureRequiredPermissionsAvailableInAccessTokenAudience ( GetType ( ) , gex . AccessToken ) ;
109
+ WriteWarning ( warningAttribute . DeprecationMessage ) ;
88
110
}
89
111
}
90
- if ( string . IsNullOrWhiteSpace ( errorMessage ) && gex . HttpResponse != null && gex . HttpResponse . StatusCode == System . Net . HttpStatusCode . Forbidden )
91
- {
92
- errorMessage = "Access denied. Check for the required permissions." ;
93
- }
94
- throw new PSInvalidOperationException ( errorMessage ) ;
95
112
}
96
113
}
97
114
98
- protected override void StopProcessing ( )
115
+ /// <summary>
116
+ /// Checks if a parameter with the provided name has been provided in the execution command
117
+ /// </summary>
118
+ /// <param name="parameterName">Name of the parameter to validate if it has been provided in the execution command</param>
119
+ /// <returns>True if a parameter with the provided name is present, false if it is not</returns>
120
+ public bool ParameterSpecified ( string parameterName )
99
121
{
100
- base . StopProcessing ( ) ;
122
+ return MyInvocation . BoundParameters . ContainsKey ( parameterName ) ;
123
+ }
124
+
125
+ #endregion
126
+
127
+ #region Logging
128
+
129
+ /// <summary>
130
+ /// Allows logging an error
131
+ /// </summary>
132
+ /// <param name="exception">The exception to log as an error</param>
133
+ internal void WriteError ( Exception exception )
134
+ {
135
+ WriteError ( exception . Message ) ;
101
136
}
102
137
103
- internal void WriteError ( Exception exception , ErrorCategory errorCategory , object target = null )
138
+ /// <summary>
139
+ /// Allows logging an error
140
+ /// </summary>
141
+ /// <param name="message">The message to log</param>
142
+ internal void WriteError ( string message )
143
+ {
144
+ Utilities . Logging . LoggingUtility . Error ( this , message , correlationId : CorrelationId ) ;
145
+ }
146
+
147
+ /// <summary>
148
+ /// Allows logging a debug message
149
+ /// </summary>
150
+ /// <param name="message">The message to log</param>
151
+ internal void LogDebug ( string message )
152
+ {
153
+ Utilities . Logging . LoggingUtility . Debug ( this , message , correlationId : CorrelationId ) ;
154
+ }
155
+
156
+ /// <summary>
157
+ /// Allows logging a warning
158
+ /// </summary>
159
+ /// <param name="message">The message to log</param>
160
+ internal void LogWarning ( string message )
161
+ {
162
+ Utilities . Logging . LoggingUtility . Warning ( this , message , correlationId : CorrelationId ) ;
163
+ }
164
+
165
+ /// <summary>
166
+ /// Allows logging an informational message
167
+ /// </summary>
168
+ /// <param name="message">The message to log</param>
169
+ internal void LogInformational ( string message )
104
170
{
105
- WriteError ( new ErrorRecord ( exception , string . Empty , errorCategory , target ) ) ;
171
+ Utilities . Logging . LoggingUtility . Info ( this , message , correlationId : CorrelationId ) ;
106
172
}
173
+
174
+ #endregion
107
175
}
108
176
}
0 commit comments