/// <summary>
        /// Processes the credentials.
        /// </summary>
        /// <param name="token">The token.</param>
        internal static void ProcessCredentials(AzureDevOpsPatToken token)
        {
            if (token.MachineScopeId == Guid.Empty &&
                !token.NotOnMachines.Contains(ConfigurationHelpers.GetMachineId()))
            {
                var cred = CredentialManager.ReadCredential(token.CredentialManagerId);

                if (cred != default(Credential))
                {
                    token.MachineScopeId = ConfigurationHelpers.GetMachineId();
                }
                else
                {
                    token.NotOnMachines.Add(ConfigurationHelpers.GetMachineId());
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:AzureDevOpsMgmt.Models.CurrentConnection" /> class.
 /// </summary>
 /// <param name="account">The account.</param>
 /// <param name="token">The token.</param>
 /// <param name="projectName">Name of the project.</param>
 public CurrentConnection(AzureDevOpsAccount account, AzureDevOpsPatToken token, string projectName)
 {
     this.Account     = account;
     this.Token       = token;
     this.ProjectName = projectName;
 }
        public void OnImport()
        {
            var accountData   = this.LoadAccountData();
            var configuration = this.LoadUserConfiguration();

            AzureDevOpsConfiguration.Config.Accounts      = accountData;
            AzureDevOpsConfiguration.Config.Configuration = configuration;

            if ((configuration.DefaultAccount != null) & (configuration.DefaultProject != null))
            {
                AzureDevOpsAccount  defaultAccount  = null;
                AzureDevOpsPatToken defaultPatToken = null;

                try
                {
                    defaultAccount  = accountData.Accounts.First(a => a.FriendlyName == configuration.DefaultAccount);
                    defaultPatToken = accountData.PatTokens.First(a => defaultAccount.LinkedPatTokens.Contains(a.Id));
                }
                catch (InvalidOperationException ioe) when(ioe.Message == "Sequence contains no matching element")
                {
                    this.ResetUserDefaultSettings();

                    var warningParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            "Corruption encountered well loading user default settings!  Settings have been reset to default (empty) values and will need to be configured again."
                        }
                    };

                    this.InvokePsCommand("Write-Warning", warningParams);
                }

                if (defaultAccount != null && defaultPatToken != null)
                {
                    AzureDevOpsConfiguration.Config.CurrentConnection = new CurrentConnection(defaultAccount, defaultPatToken, configuration.DefaultProject);

                    var outputParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            $"Default Account Settings Loaded Successfully.\r\nAccount Name: {configuration.DefaultAccount}\r\nProject Name: {configuration.DefaultProject}"
                        }
                    };
                    this.InvokePsCommand("Write-Host", outputParams);
                }
            }

            if (!AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade)
            {
                foreach (var token in AzureDevOpsConfiguration.Config.Accounts.PatTokens)
                {
                    ModuleStartup.ProcessCredentials(token);
                }

                AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade = true;
                FileHelpers.WriteFileJson(FileNames.AccountData, AzureDevOpsConfiguration.Config.Accounts);
            }

            var setVariableParams = new Dictionary <string, object>()
            {
                { "Name", "AzureDevOpsConfiguration" },
                { "Value", AzureDevOpsConfiguration.Config }
            };

            this.InvokePsCommand("Set-Variable", setVariableParams);
        }