/// <summary> /// Sets the nebulon connection if it is set globally in the /// PowerShell session and fails if no connection is found. /// </summary> /// <exception cref="ArgumentException"> /// If no connection is established /// </exception> private void LoadConnection() { if (Connection == null) { Connection = (NebConnection)SessionState .PSVariable .GetValue("NebulonConnection"); } if (Connection == null) { throw new ArgumentException("Please login first"); } }
/// <summary> /// Performs execution of the command /// </summary> protected override void ProcessRecord() { try { // create a new connection. This will overwrite any existing // connection that is already established NebConnection connection = new NebConnection(); connection.Logger = new PowerShellLogger(this); // add support for the default parameter '-Verbose' if (MyInvocation.BoundParameters.ContainsKey("Verbose")) { connection.Logger.LogLevel = LogSeverity.Verbose; } // add support for the default parameter '-Debug' if (MyInvocation.BoundParameters.ContainsKey("Debug")) { connection.Logger.LogLevel = LogSeverity.Debug; } NetworkCredential credential = ParameterSetName == @"UserName" ? new NetworkCredential(UserName, Password) : new NetworkCredential(Credential.UserName, Credential.Password); LoginResults loginResults = connection.Login(credential.UserName, credential.Password); WriteVerbose(loginResults.Message); WriteVerbose(loginResults.Organization); // login was successful so we can store the connection as // a global variable SessionState.PSVariable.Set( new PSVariable("NebulonConnection", connection, ScopedItemOptions.AllScope)); WriteObject(connection); } catch (AggregateException exceptions) { foreach (Exception ex in exceptions.InnerExceptions) { ErrorRecord record = new ErrorRecord( ex, ex.GetType().ToString(), ErrorCategory.NotSpecified, null); WriteError(record); } } catch (Exception ex) { ErrorRecord record = new ErrorRecord( ex, ex.GetType().ToString(), ErrorCategory.NotSpecified, null); WriteError(record); } }