protected override void ProcessRecord() { base.ProcessRecord(); try { using (var asyncCommandRuntime = new CustomAsyncCommandRuntime(this, _cancellationTokenSource.Token)) { // Init output to this Cmdlet GraphSessionInitializer.InitializeOutput(asyncCommandRuntime); asyncCommandRuntime.Wait(ProcessRecordAsync(), _cancellationTokenSource.Token); } } catch (AggregateException aggregateException) { // unroll the inner exceptions to get the root cause foreach (var innerException in aggregateException.Flatten().InnerExceptions) { var errorRecords = innerException.Data; if (errorRecords.Count > 1) { foreach (DictionaryEntry dictionaryEntry in errorRecords) { WriteError((ErrorRecord)dictionaryEntry.Value); } } else { WriteError(new ErrorRecord(innerException, string.Empty, ErrorCategory.NotSpecified, null)); } } } catch (Exception exception) when(exception as PipelineStoppedException == null || (exception as PipelineStoppedException).InnerException != null) { // Write exception out to error channel. WriteError(new ErrorRecord(exception, string.Empty, ErrorCategory.NotSpecified, null)); } }