Exemplo n.º 1
0
        protected void GetSession()
        {
            sessions = CommonCmdletFunctions.GetAllSessions(this);

            if (sessions.Count == 0)
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("Could not find open sessions to any XenServers."),
                                          "",
                                          ErrorCategory.InvalidArgument, null));
            }

            session = null;

            if (string.IsNullOrEmpty(SessionOpaqueRef))
            {
                if (sessions.Count == 1)
                {
                    foreach (KeyValuePair <string, Session> kvp in sessions)
                    {
                        session = kvp.Value;
                    }
                }
                else
                {
                    Session defaultSession = CommonCmdletFunctions.GetDefaultXenSession(this);

                    if (sessions.ContainsValue(defaultSession))
                    {
                        session = defaultSession;
                    }

                    if (session == null)
                    {
                        ThrowTerminatingError(new ErrorRecord(
                                                  new Exception("A default XenServer session has not beeen set."),
                                                  "",
                                                  ErrorCategory.InvalidArgument, null));
                    }
                }
            }
            else
            {
                if (sessions.ContainsKey(SessionOpaqueRef))
                {
                    session = sessions[SessionOpaqueRef];
                }

                if (session == null)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("Could not locate the specified session in the open XenServer sessions."),
                                              "",
                                              ErrorCategory.InvalidArgument, SessionOpaqueRef));
                }
            }
        }
        protected override void ProcessRecord()
        {
            var sessions = CommonCmdletFunctions.GetAllSessions(this);

            if (sessions.Count == 0)
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("Could not find open sessions to any XenServers."),
                                          "",
                                          ErrorCategory.InvalidArgument, null));
            }

            Session session = null;

            if (Session == null && Ref == null)
            {
                if (sessions.Count == 1)
                {
                    foreach (KeyValuePair <string, Session> kvp in sessions)
                    {
                        session = kvp.Value;
                    }
                }
                else
                {
                    Session defaultSession = CommonCmdletFunctions.GetDefaultXenSession(this);

                    if (sessions.ContainsValue(defaultSession))
                    {
                        session = defaultSession;
                    }

                    if (session == null)
                    {
                        ThrowTerminatingError(new ErrorRecord(
                                                  new Exception("A default XenServer session has not beeen set."),
                                                  "",
                                                  ErrorCategory.InvalidArgument, null));
                    }
                }
            }
            else
            {
                if (Session != null && sessions.ContainsKey(Session.opaque_ref))
                {
                    session = Session;
                }
                else if (Ref != null && sessions.ContainsKey(Ref.opaque_ref))
                {
                    session = sessions[Ref.opaque_ref];
                }

                if (session == null)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new Exception("Could not locate the specified session in the open XenServer sessions."),
                                              "",
                                              ErrorCategory.InvalidArgument,
                                              Session != null ? Session.opaque_ref : Ref.opaque_ref));
                }
            }

            try
            {
                session.logout();
            }
            finally
            {
                WriteVerbose("Removing session from Citrix.XenServer.Sessions variable.");
                sessions.Remove(session.opaque_ref);
            }
        }