/// <summary> /// Initializes <see cref="GraphSession"/>. with Output via Cmdlet methods /// </summary> /// <param name="cmdLet"></param> internal static void InitializeOutput(CustomAsyncCommandRuntime cmdLet) { var outputWriter = new PsGraphOutputWriter { WriteDebug = cmdLet.WriteDebug, WriteInformation = (o, strings) => { cmdLet.WriteInformation(new InformationRecord(o, strings)); }, WriteObject = cmdLet.WriteObject, WriteVerbose = cmdLet.WriteVerbose, WriteError = (exception, errorId, errorCategory, targetObject) => { var parseResult = Enum.TryParse(errorCategory.ToString(), out ErrorCategory result); if (!parseResult) { result = ErrorCategory.NotSpecified; } var errorRecord = new ErrorRecord(exception, errorId, result, targetObject); cmdLet.WriteError(errorRecord); } }; InitializeOutput(outputWriter); }
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)); } }