protected override void ProcessRecord() { InfobloxSessionData.Reset(); if (this.Version.Equals("LATEST")) { using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, "1.0", this.Credential.UserName, this.Credential.Password).Result) { WriteVerbose("Getting supported versions."); try { //Because I'm using .Result instead of await, the exception thrown is an AggregrateException //instead of a normal exception if we'd used await like IBXCommonMethods.GetAsnyc() does //So the resulting InfobloxCustomException isn't as informative, may need to update logic //to use the InvokeGenericAsync() logic being used in PSExtensionMethods to keep ProcessRecord //as a sync function HttpResponseMessage Response = Client.GetAsync("?_schema").Result; WriteVerbose(Response.RequestMessage.RequestUri.ToString()); if (Response.IsSuccessStatusCode) { string Content = Response.Content.ReadAsStringAsync().Result; WriteVerbose($"Response {Content}"); dynamic Obj = JsonConvert.DeserializeObject(Content); JArray JVersions = Obj.supported_versions; IEnumerable <string> Versions = JVersions.ToObject <string[]>(); WriteVerbose("Got versions"); Versions = Versions.Select(x => { return(new Version(x)); }).OrderByDescending(x => x).Select(x => { return(x.ToString()); }); WriteVerbose("Sorted versions"); this.Version = Versions.First(); WriteVerbose($"Latest supported version is {this.Version}"); } else { WriteVerbose("Failed to get schema, reverting to using version 2.0"); this.Version = "2.0"; } } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (AggregateException e) { InfobloxCustomException Ex = new InfobloxCustomException(e.InnerException.Message); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } } } WriteVerbose("Infoblox session data will be used now."); InfobloxSessionData.GridMaster = this.GridMaster; InfobloxSessionData.Credential = new InfobloxCredential(this.Credential.UserName, this.Credential.Password); InfobloxSessionData.Version = this.Version; InfobloxSessionData.UseSessionData = true; using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, this.Version, this.Credential.UserName, this.Credential.Password).Result) { try { HttpResponseMessage Response = Client.GetAsync("grid").Result; Cookie Cookie = CommandHelpers.GetResponseCookie(Response); if (Cookie != null) { WriteVerbose("Infoblox cookie will be used for authentication."); InfobloxSessionData.Cookie = Cookie; } else { WriteWarning("Could not retrieve a valid cookie from the grid master."); } } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } } }
protected override void ProcessRecord() { if (InfobloxSessionData.UseSessionData && InfobloxSessionData.Cookie != null) { if (!InfobloxSessionData.Cookie.Expired) { using (HttpClient Client = CommandHelpers.BuildHttpClient(InfobloxSessionData.GridMaster, InfobloxSessionData.Version).Result) { try { HttpResponseMessage Response = Client.PostAsync("logout", null).Result; if (Response.IsSuccessStatusCode) { WriteVerbose("Successfully ended session."); } else { ThrowTerminatingError(new ErrorRecord(new WebException(Response.StatusCode.ToString()), Response.StatusCode.ToString(), ErrorCategory.InvalidOperation, Client.BaseAddress)); } } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } } } else if (InfobloxSessionData.Credential != null) { using (HttpClient Client = CommandHelpers.BuildHttpClient(InfobloxSessionData.GridMaster, InfobloxSessionData.Version, InfobloxSessionData.Credential.UserName, InfobloxSessionData.Credential.Password).Result) { try { HttpResponseMessage Response = Client.PostAsync("logout", null).Result; if (Response.IsSuccessStatusCode) { WriteVerbose("Successfully ended session."); } else { ThrowTerminatingError(new ErrorRecord(new WebException(Response.StatusCode.ToString()), Response.StatusCode.ToString(), ErrorCategory.InvalidOperation, Client.BaseAddress)); } } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } } } else { WriteWarning($"No valid authentication methods present to use to logout of Infoblox session to {InfobloxSessionData.GridMaster}"); } } InfobloxSessionData.Reset(); }
/// <summary> /// Writes an IEnumerable of Exceptions to the provided PSHost console. This /// function allows us to print in both PowerShell ISE and the regular PowerShell /// console. It tries to get a PSHost even if one isn't provided /// </summary> /// <param name="exceptions">The exceptions to print</param> /// <param name="host">The PSHost to write the content out to.</param> private static void WriteExceptions(IEnumerable <Exception> exceptions, PSHost host = null) { if (exceptions != null) { if (host == null) { Runspace Runspace = Runspace.DefaultRunspace; //Attempt to get the default host value if (Runspace != null) { Pipeline NewPipeline = Runspace.CreateNestedPipeline("Get-Variable host -ValueOnly", false); /* The returned object from the Cmdlet: * Name : ConsoleHost * * Name : Windows PowerShell ISE Host * Version : 5.1.15063.138 * InstanceId : 71ed23e7-7b80-4273-a3d7-a0ab70dfb21b * UI : System.Management.Automation.Internal.Host.InternalHostUserInterface * CurrentCulture : en-US * CurrentUICulture : en-US * PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions * DebuggerEnabled : True * IsRunspacePushed : False * Runspace : System.Management.Automation.Runspaces.LocalRunspace */ Collection <PSObject> Results = NewPipeline.Invoke(); if (Results.Count > 0) { host = Results[0].BaseObject as PSHost; } } } //If a default host was found or one was provided if (host != null) { PSHostUserInterface UserInterface = host.UI; foreach (Exception Ex in exceptions) { UserInterface.WriteLine(); UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"ERROR: {Ex.GetType().FullName}"); if (Ex.GetType() == typeof(InfobloxCustomException)) { InfobloxCustomException ce = (InfobloxCustomException)Ex; UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"INFOBLOX ERROR: {ce.Error}"); UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"INFOBLOX TEXT: {ce.Text}"); UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"HTTP STATUS CODE: {ce.HttpResponseCode}"); UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"HTTP STATUS: {ce.HttpStatus}"); UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"HTTP ERROR REASON: {ce.HttpErrorReason}"); } else { UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"MESSAGE: {Ex.Message}"); } if (!String.IsNullOrEmpty(Ex.StackTrace)) { UserInterface.WriteLine(ConsoleColor.Red, ConsoleColor.Black, $"STACK TRACE: {Ex.StackTrace}"); } UserInterface.WriteLine(); } } else //Otherwise, just use console write { foreach (Exception Ex in exceptions) { ConsoleColor OriginalBackground = Console.BackgroundColor; ConsoleColor OriginalForeground = Console.ForegroundColor; Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine($"ERROR: {Ex.GetType().FullName}"); if (Ex.GetType() == typeof(InfobloxCustomException)) { InfobloxCustomException IbxEx = (InfobloxCustomException)Ex; Console.WriteLine($"INFOBLOX ERROR: {IbxEx.Error}"); Console.WriteLine($"INFOBLOX TEXT: {IbxEx.Text}"); Console.WriteLine($"HTTP STATUS CODE: {IbxEx.HttpResponseCode}"); Console.WriteLine($"HTTP STATUS: {IbxEx.HttpStatus}"); Console.WriteLine($"HTTP ERROR REASON: {IbxEx.HttpErrorReason}"); } else { Console.WriteLine($"MESSAGE: {Ex.Message}"); } if (!String.IsNullOrEmpty(Ex.StackTrace)) { Console.WriteLine($"STACK TRACE: {Ex.StackTrace}"); } //Reset console colors Console.BackgroundColor = OriginalBackground; Console.ForegroundColor = OriginalForeground; Console.WriteLine(); } } } else { throw new ArgumentNullException("exceptions", "The list of exceptions to print cannot be null"); } }
protected override void ProcessRecord() { if (InfobloxSessionData.Version.Equals("LATEST")) { using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, "1.0", this.Credential.UserName, this.Credential.Password).Result) { WriteVerbose("Getting supported versions."); try { HttpResponseMessage Response = Client.GetAsync("?_schema").Result; WriteVerbose(Response.RequestMessage.RequestUri.ToString()); if (Response.IsSuccessStatusCode) { string Content = Response.Content.ReadAsStringAsync().Result; WriteVerbose($"Response {Content}"); dynamic Obj = JsonConvert.DeserializeObject(Content); IEnumerable <string> Versions = Obj.supported_versions; WriteVerbose("Got versions"); Versions = Versions.Select(x => { return(new Version(x)); }).OrderByDescending(x => x).Select(x => { return(x.ToString()); }); WriteVerbose("Sorted versions"); this.Version = Versions.First(); WriteVerbose($"Latest supported version is {this.Version}"); } else { WriteVerbose("Failed to get schema, reverting to using version 2.0"); this.Version = "2.0"; } } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); } } } InfobloxSession Session = new InfobloxSession() { GridMaster = this.GridMaster, Credential = new InfobloxCredential(this.Credential.UserName, this.Credential.Password), Version = this.Version }; using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, this.Version, this.Credential.UserName, this.Credential.Password).Result) { try { HttpResponseMessage Response = Client.GetAsync("grid").Result; Cookie Cookie = CommandHelpers.GetResponseCookie(Response); if (Cookie != null && !Cookie.Expired) { Session.Cookie = Cookie; } WriteObject(Session); } catch (WebException e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (HttpRequestException e) { InfobloxCustomException Ex; if (e.InnerException is WebException) { Ex = new InfobloxCustomException((WebException)e.InnerException); } else { Ex = new InfobloxCustomException(e); } PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } catch (Exception e) { InfobloxCustomException Ex = new InfobloxCustomException(e); PSCommon.WriteExceptions(Ex, this.Host); this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this)); } } }