45
45
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
46
46
using Microsoft . Azure . PowerShell . Common . Share . Survey ;
47
47
using Microsoft . Azure . Commands . Profile . Utilities ;
48
+ using System . Management . Automation . Runspaces ;
48
49
49
50
namespace Microsoft . Azure . Commands . Profile
50
51
{
@@ -249,6 +250,7 @@ protected override IAzureContext DefaultContext
249
250
protected override void BeginProcessing ( )
250
251
{
251
252
base . BeginProcessing ( ) ;
253
+ ValidateActionRequiredMessageCanBePresented ( ) ;
252
254
if ( AzureEnvironment . PublicEnvironments . ContainsKey ( EnvironmentName . AzureCloud ) )
253
255
{
254
256
_environment = AzureEnvironment . PublicEnvironments [ EnvironmentName . AzureCloud ] ;
@@ -273,11 +275,19 @@ protected override void BeginProcessing()
273
275
274
276
_writeWarningEvent -= WriteWarningSender ;
275
277
_writeWarningEvent += WriteWarningSender ;
278
+ _writeInformationEvent -= WriteInformationSender ;
279
+ _writeInformationEvent += WriteInformationSender ;
280
+
276
281
// store the original write warning handler, register a thread safe one
277
282
AzureSession . Instance . TryGetComponent ( WriteWarningKey , out _originalWriteWarning ) ;
278
283
AzureSession . Instance . UnregisterComponent < EventHandler < StreamEventArgs > > ( WriteWarningKey ) ;
279
284
AzureSession . Instance . RegisterComponent ( WriteWarningKey , ( ) => _writeWarningEvent ) ;
280
285
286
+ // store the original write information handler, register a thread safe one
287
+ AzureSession . Instance . TryGetComponent ( WriteInformationKey , out _originalWriteInformation ) ;
288
+ AzureSession . Instance . UnregisterComponent < EventHandler < StreamEventArgs > > ( WriteInformationKey ) ;
289
+ AzureSession . Instance . RegisterComponent ( WriteInformationKey , ( ) => _writeInformationEvent ) ;
290
+
281
291
// todo: ideally cancellation token should be passed to authentication factory as a parameter
282
292
// however AuthenticationFactory.Authenticate does not support it
283
293
// so I store it in AzureSession.Instance as a global variable
@@ -289,11 +299,19 @@ protected override void BeginProcessing()
289
299
private event EventHandler < StreamEventArgs > _writeWarningEvent ;
290
300
private event EventHandler < StreamEventArgs > _originalWriteWarning ;
291
301
302
+ private event EventHandler < StreamEventArgs > _writeInformationEvent ;
303
+ private event EventHandler < StreamEventArgs > _originalWriteInformation ;
304
+
292
305
private void WriteWarningSender ( object sender , StreamEventArgs args )
293
306
{
294
307
_tasks . Enqueue ( new Task ( ( ) => this . WriteWarning ( args . Message ) ) ) ;
295
308
}
296
309
310
+ private void WriteInformationSender ( object sender , StreamEventArgs args )
311
+ {
312
+ _tasks . Enqueue ( new Task ( ( ) => this . WriteInformation ( args . Message ) ) ) ;
313
+ }
314
+
297
315
protected override void StopProcessing ( )
298
316
{
299
317
if ( AzureSession . Instance . TryGetComponent ( "LoginCancellationToken" , out CancellationTokenSource cancellationTokenSource ) )
@@ -562,6 +580,20 @@ public override void ExecuteCmdlet()
562
580
}
563
581
}
564
582
583
+ private void ValidateActionRequiredMessageCanBePresented ( )
584
+ {
585
+ if ( UseDeviceAuthentication . IsPresent && IsWriteInformationIgnored ( ) )
586
+ {
587
+ throw new ActionPreferenceStopException ( Resources . DoNotIgnoreInformationIfUserDeviceAuth ) ;
588
+ }
589
+ }
590
+
591
+ private bool IsWriteInformationIgnored ( )
592
+ {
593
+ return ! MyInvocation . BoundParameters . ContainsKey ( "InformationAction" ) && ActionPreference . Ignore . ToString ( ) . Equals ( SessionState ? . PSVariable ? . GetValue ( "InformationPreference" , ActionPreference . SilentlyContinue ) ? . ToString ( ) ?? "" ) ||
594
+ MyInvocation . BoundParameters . TryGetValue ( "InformationAction" , out var value ) && ActionPreference . Ignore . ToString ( ) . Equals ( value ? . ToString ( ) ?? "" , StringComparison . InvariantCultureIgnoreCase ) ;
595
+ }
596
+
565
597
private string PreProcessAuthScope ( )
566
598
{
567
599
string mappedScope = AuthScope ;
@@ -774,6 +806,9 @@ protected override void EndProcessing()
774
806
// unregister the thread-safe write warning, because it won't work out of this cmdlet
775
807
AzureSession . Instance . UnregisterComponent < EventHandler < StreamEventArgs > > ( WriteWarningKey ) ;
776
808
AzureSession . Instance . RegisterComponent ( WriteWarningKey , ( ) => _originalWriteWarning ) ;
809
+ // unregister the thread-safe write information, because it won't work out of this cmdlet
810
+ AzureSession . Instance . UnregisterComponent < EventHandler < StreamEventArgs > > ( WriteInformationKey ) ;
811
+ AzureSession . Instance . RegisterComponent ( WriteInformationKey , ( ) => _originalWriteInformation ) ;
777
812
}
778
813
}
779
814
}
0 commit comments