public override void ExecuteCmdlet() { PSCredential creds = null; if (Credentials != null) { creds = Credentials.Credential; } if (ParameterSetName == "Token") { SPIaCConnection.CurrentConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), Realm, AppId, AppSecret, AppDomain, ResourceUri, Host, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); } else if (ParameterSetName == "CredentialCache") { creds = CredentialManager.GetCredential(CredentialName); //var genericcreds = CredentialManager.GetCredential(CredentialName); //creds = new PSCredential(genericcreds.UserName, genericcreds.SecurePassword); SPIaCConnection.CurrentConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); } else { if (!CurrentCredentials && creds == null) { creds = Host.UI.PromptForCredential(Resources.EnterYourCredentials, "", "", ""); } LogVerbose("Received credentials for {0} user", creds.UserName); var initializedConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); if (initializedConnection == null) { throw new Exception(string.Format("Error establishing a connection to {0}. Check the diagnostic logs.", Url)); } SPIaCConnection.CurrentConnection = initializedConnection; LogVerbose("Processed credentials for {0} user", creds.UserName); } }
protected override void ProcessRecord() { PSCredential creds = null; if (Credentials != null) { creds = Credentials.Credential; } if (ParameterSetName == "Token") { SPIaCConnection.CurrentConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), Realm, AppId, AppSecret, Host, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); } else if (ParameterSetName == "CredentialCache") { var genericcreds = CredentialManager.GetCredential(CredentialName); creds = new PSCredential(genericcreds.UserName, genericcreds.SecurePassword); SPIaCConnection.CurrentConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); } else if (ParameterSetName == "UserCache") { var boolSaveToDisk = false; var runningDirectory = this.SessionState.Path.CurrentFileSystemLocation; var userPasswordConfig = string.Format("{0}\\{1}.pswd", runningDirectory, UserName).Replace("\\", @"\"); if (System.IO.File.Exists(userPasswordConfig)) { var encryptedUserPassword = System.IO.File.ReadAllText(userPasswordConfig); var encryptedSecureString = encryptedUserPassword.ConvertToSecureString(); if (!CurrentCredentials && creds == null) { if (encryptedSecureString == null || encryptedSecureString.Length <= 0) { boolSaveToDisk = true; creds = Host.UI.PromptForCredential(Resources.EnterYourCredentials, "", UserName, ""); } else { creds = new PSCredential(this.UserName, encryptedSecureString); } } } else { // the password was not encrypted if (!CurrentCredentials && creds == null) { boolSaveToDisk = true; creds = Host.UI.PromptForCredential(Resources.EnterYourCredentials, "", UserName, ""); } } var initializedConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); if (initializedConnection == null) { throw new Exception(string.Format("Error establishing a connection to {0}. Check the diagnostic logs.", Url)); } SPIaCConnection.CurrentConnection = initializedConnection; if (boolSaveToDisk) { var encryptedSecureString = creds.Password.ConvertFromSecureString(); System.IO.File.WriteAllText(userPasswordConfig, encryptedSecureString); } } else { if (!CurrentCredentials && creds == null) { creds = Host.UI.PromptForCredential(Resources.EnterYourCredentials, "", "", ""); } LogVerbose("Received credentials for {0} user", creds.UserName); var initializedConnection = SPIaCConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, SkipTenantAdminCheck); if (initializedConnection == null) { throw new Exception(string.Format("Error establishing a connection to {0}. Check the diagnostic logs.", Url)); } SPIaCConnection.CurrentConnection = initializedConnection; LogVerbose("Processed credentials for {0} user", creds.UserName); } }