ContextAutosaveSettings GetAutoSaveSettings(IAzureSession session, ContextModificationScope scope) { ContextAutosaveSettings settings = null; switch (scope) { case ContextModificationScope.CurrentUser: settings = new ContextAutosaveSettings { CacheDirectory = session.TokenCacheDirectory, CacheFile = session.TokenCacheFile, ContextDirectory = session.ARMProfileDirectory, ContextFile = session.ARMProfileFile, Mode = ContextSaveMode.CurrentUser }; break; default: settings = new ContextAutosaveSettings { CacheDirectory = NoDirectory, CacheFile = NoDirectory, ContextDirectory = NoDirectory, ContextFile = NoDirectory, Mode = ContextSaveMode.Process }; break; } return(settings); }
/// <summary> /// Get the context modification scope for the current process. This will be 'Process' if changes should /// only affect the current PowerShell session, or 'CurrentUser' if any changes should be global. /// </summary> /// <param name="writer">A writer to write debug messages</param> /// <returns>'Process' if all changes should only impact the current session, or 'CurrentUser' if /// changes should be global.</returns> public static ContextModificationScope GetContextModificationScopeForProcess(DebugWriter writer) { ContextModificationScope scope = AzureSession.Instance.ARMContextSaveMode == ContextSaveMode.Process ? ContextModificationScope.Process : ContextModificationScope.CurrentUser; try { writer(Resources.AutosaveSettingFromSession, scope); var autoSaveSetting = Environment.GetEnvironmentVariable(AzureRmProfileConstants.ProfileAutoSaveVariable); bool autoSaveEnabled = false; if (autoSaveSetting != null && bool.TryParse(autoSaveSetting, out autoSaveEnabled)) { scope = autoSaveEnabled ? ContextModificationScope.CurrentUser : ContextModificationScope.Process; writer(Resources.AutosaveSettingFromEnvironment, AzureRmProfileConstants.ProfileAutoSaveVariable, autoSaveSetting); } else { writer(Resources.CouldNotRetrieveAutosaveSetting, AzureRmProfileConstants.ProfileAutoSaveVariable); } } catch (Exception exception) { writer(Resources.ErrorRetrievingAutosaveSetting, AzureRmProfileConstants.ProfileAutoSaveVariable, exception); } return(scope); }
/// <summary> /// Get the context modification scope for the current user. This will be 'Process' if changes should /// only affect the current PowerShell session, or 'CurrentUser' if any changes should be global. /// </summary> /// <param name="writer">A writer to write debug messages</param> /// <returns>'Process' if all changes should only impact the current session, or 'CurrentUser' if /// changes should be global.</returns> public static ContextModificationScope GetContextModificationScopeForUser(IAzureSession session, DebugWriter writer) { ContextModificationScope scope = ContextModificationScope.Process; try { string autoSavePath = Path.Combine(session.ProfileDirectory, ContextAutosaveSettings.AutoSaveSettingsFile); var store = session.DataStore; if (store.FileExists(autoSavePath)) { var settings = JsonConvert.DeserializeObject <ContextAutosaveSettings>(store.ReadFileAsText(autoSavePath)); if (settings != null && !string.IsNullOrWhiteSpace(settings.Mode)) { scope = GetContextModificationScopeFromSaveMode(settings.Mode); writer(Resources.AutosaveSettingFromFile, autoSavePath, settings.Mode); } } var userAutoSaveSetting = Environment.GetEnvironmentVariable(AzureRmProfileConstants.ProfileAutoSaveVariable, EnvironmentVariableTarget.User); var machineAutoSaveSetting = Environment.GetEnvironmentVariable(AzureRmProfileConstants.ProfileAutoSaveVariable, EnvironmentVariableTarget.Machine); bool autoSaveEnabled = false; if (userAutoSaveSetting != null && bool.TryParse(userAutoSaveSetting, out autoSaveEnabled)) { scope = autoSaveEnabled ? ContextModificationScope.CurrentUser : ContextModificationScope.Process; writer(Resources.AutosaveSettingFromEnvironment, AzureRmProfileConstants.ProfileAutoSaveVariable, userAutoSaveSetting); } else if (machineAutoSaveSetting != null && bool.TryParse(machineAutoSaveSetting, out autoSaveEnabled)) { scope = autoSaveEnabled ? ContextModificationScope.CurrentUser : ContextModificationScope.Process; writer(Resources.AutosaveSettingFromEnvironment, AzureRmProfileConstants.ProfileAutoSaveVariable, machineAutoSaveSetting); } else { writer(Resources.CouldNotRetrieveAutosaveSetting, AzureRmProfileConstants.ProfileAutoSaveVariable); } } catch (Exception exception) { writer(Resources.ErrorRetrievingAutosaveSetting, AzureRmProfileConstants.ProfileAutoSaveVariable, exception); } return(scope); }
/// <summary> /// Get the context modification scope for the current cmdlet invoication /// </summary> /// <returns>Process if the cmdlet should only change the current process, CurrentUser /// if any changes should occur globally.</returns> protected virtual ContextModificationScope GetContextModificationScope() { ContextModificationScope scope = ScopeHelpers.GetContextModificationScopeForProcess(WriteDebugWithTimestamp); // override default scope with appropriate scope for thsi cmdlet invocation if (MyInvocation != null && MyInvocation.BoundParameters != null && MyInvocation.BoundParameters.ContainsKey(nameof(DefaultProfile))) { // never autosave with a passed-in profile scope = ContextModificationScope.Process; WriteDebugWithTimestamp(Resources.AutosaveDisabledForContextParameter); } else if (MyInvocation != null && MyInvocation.BoundParameters != null && MyInvocation.BoundParameters.ContainsKey(nameof(Scope))) { scope = Scope; WriteDebugWithTimestamp(Resources.AutosaveSettingFromScope, scope); } WriteDebugWithTimestamp(Resources.AutosaveSettingFinalValue, scope); return(scope); }
/// <summary> /// Translate the ContextModificationScope enum into a string /// </summary> /// <param name="scope">The scope to translate</param> /// <returns>A string representation of the scope</returns> public static string GetContextSaveMode(ContextModificationScope scope) { return(scope == ContextModificationScope.CurrentUser? ContextSaveMode.CurrentUser : ContextSaveMode.Process); }