Ejemplo n.º 1
0
        protected override void ProcessRecord()
        {
            if ((Url == null || Url.Length == 0) && (Server == null || Server.Length == 0))
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("You must provide a URL, Name or IP Address for the XenServer."),
                                          "",
                                          ErrorCategory.InvalidArgument,
                                          null));
            }

            if (Creds == null &&
                (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)) &&
                (OpaqueRef == null || OpaqueRef.Length == 0))
            {
                Creds = Host.UI.PromptForCredential("XenServer Credential Request",
                                                    "",
                                                    string.IsNullOrEmpty(UserName) ? "root" : UserName,
                                                    "");

                if (Creds == null)
                {
                    // Just bail out at this point, they've clicked cancel on the credentials pop up dialog
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("Credentials must be supplied when connecting to the XenServer."),
                                              "",
                                              ErrorCategory.InvalidArgument,
                                              null));
                }
            }

            string connUser     = "";
            string connPassword = "";

            if (OpaqueRef == null || OpaqueRef.Length == 0)
            {
                if (Creds == null)
                {
                    connUser     = UserName;
                    connPassword = Password;

                    SecureString secPwd = new SecureString();
                    foreach (char ch in connPassword)
                    {
                        secPwd.AppendChar(ch);
                    }

                    Creds = new PSCredential(UserName, secPwd);
                }
                else
                {
                    connUser = Creds.UserName.StartsWith("\\")
                                   ? Creds.GetNetworkCredential().UserName
                                   : Creds.UserName;

                    IntPtr ptrPassword = Marshal.SecureStringToBSTR(Creds.Password);
                    connPassword = Marshal.PtrToStringBSTR(ptrPassword);
                    Marshal.FreeBSTR(ptrPassword);
                }
            }

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

            if (Url == null || Url.Length == 0)
            {
                Url = new string[Server.Length];
                for (int i = 0; i < Server.Length; i++)
                {
                    Url[i] = CommonCmdletFunctions.GetUrl(Server[i], Port);
                }
            }

            if (OpaqueRef == null)
            {
                OpaqueRef = new string[Url.Length];
            }
            else
            {
                if (OpaqueRef.Length != Url.Length)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("The number of opaque references provided should be the same as the number of xenservers."),
                                              "",
                                              ErrorCategory.InvalidArgument,
                                              null));
                }
            }

            Dictionary <string, Session> sessions    = CommonCmdletFunctions.GetAllSessions(this);
            Dictionary <string, Session> newSessions = new Dictionary <string, Session>();

            for (int i = 0; i < Url.Length; i++)
            {
                Session session;
                if (string.IsNullOrEmpty(OpaqueRef[i]))
                {
                    session = new Session(Url[i]);
                    session.login_with_password(connUser, connPassword);
                }
                else
                {
                    session = new Session(Url[i], OpaqueRef[i]);
                }

                session.Tag                     = Creds;
                session.opaque_ref              = session.uuid;
                sessions[session.opaque_ref]    = session;
                newSessions[session.opaque_ref] = session;

                if (i > 0)
                {
                    continue;
                }

                //set the first of the specified connections as default
                if (SetDefaultSession)
                {
                    WriteVerbose(string.Format("Setting connection {0} ({1}) as default.", session.Url, session.opaque_ref));
                    CommonCmdletFunctions.SetDefaultXenSession(this, session);
                }
            }

            CommonCmdletFunctions.SetAllSessions(this, sessions);

            if (PassThru)
            {
                WriteObject(newSessions.Values, true);
            }
        }
Ejemplo n.º 2
0
 protected void UpdateSessions()
 {
     //save session dictionary back in the session variable (in case it was modified)
     CommonCmdletFunctions.SetAllSessions(this, sessions);
 }