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() { Dictionary <string, Session> sessions = CommonCmdletFunctions.GetAllSessions(this); if (string.IsNullOrEmpty(Url) && !string.IsNullOrEmpty(Server)) { Url = CommonCmdletFunctions.GetUrl(Server, Port); } if (!string.IsNullOrEmpty(Ref)) { if (sessions.ContainsKey(Ref.opaque_ref)) { WriteObject(sessions[Ref.opaque_ref]); } } else if (!string.IsNullOrEmpty(Url)) { List <Session> results = new List <Session>(); foreach (KeyValuePair <string, Session> kvp in sessions) { if (kvp.Value.Url == Url) { results.Add(kvp.Value); } } WriteObject(results, true); } else if (!string.IsNullOrEmpty(UserName)) { List <Session> results = new List <Session>(); foreach (KeyValuePair <string, Session> kvp in sessions) { PSCredential cred = kvp.Value.Tag as PSCredential; if (cred != null && cred.UserName == UserName) { results.Add(kvp.Value); } } WriteObject(results, true); } else { WriteObject(sessions.Values, true); } }
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); } }
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); } }