Пример #1
0
        protected override void ProcessRecord()
        {
            FileInfo                   file            = string.IsNullOrEmpty(this.File) ? (FileInfo)null : new FileInfo(this.File);
            string                     str1            = string.IsNullOrEmpty(this.Host) ? "*" : this.Host;
            WildcardPattern            hostPattern     = new WildcardPattern(str1, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
            string                     str2            = string.IsNullOrEmpty(this.User) ? "*" : this.User;
            WildcardPattern            usernamePattern = new WildcardPattern(str2, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
            List <CredentialStoreItem> list            = new List <CredentialStoreItem>();

            try
            {
                ICredentialStore credentialStore = (ICredentialStore)null;
                try
                {
                    credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
                    foreach (string str3 in credentialStore.GetHosts())
                    {
                        if (hostPattern.IsMatch(str3))
                        {
                            foreach (string str4 in credentialStore.GetUsernames(str3))
                            {
                                if (usernamePattern.IsMatch(str4))
                                {
                                    string password = new string(credentialStore.GetPassword(str3, str4));
                                    list.Add((CredentialStoreItem) new CredentialStoreItemImpl(str3, str4, password, this.File));
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (credentialStore != null)
                    {
                        credentialStore.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                this.ThrowTerminatingError(new ErrorRecord(ex, "Core_GetCredentialStoreItem_ProcessRecordget", System.Management.Automation.ErrorCategory.NotSpecified, (object)null));
            }
            this.WriteObject((object)list, true);
            this.ReportErrorIfNotFound(str1, str2, hostPattern, usernamePattern, (IEnumerable <CredentialStoreItem>)list);
        }
Пример #2
0
        protected override void ProcessRecord()
        {
            FileInfo file = string.IsNullOrEmpty(this.File) ? (FileInfo)null : new FileInfo(this.File);
            List <CredentialStoreItemImpl> list = new List <CredentialStoreItemImpl>();
            ICredentialStore credentialStore    = (ICredentialStore)null;

            try
            {
                credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
                if (this.Password == null)
                {
                    this.Password = "";
                }
                credentialStore.AddPassword(this.Host, this.User, this.Password.ToCharArray());
                list.Add(new CredentialStoreItemImpl(this.Host, this.User, new string(credentialStore.GetPassword(this.Host, this.User)), this.File));
            }
            finally
            {
                if (credentialStore != null)
                {
                    credentialStore.Close();
                }
            }
            this.WriteObject((object)list, true);
        }
Пример #3
0
 protected override void ProcessRecord()
 {
     if (this.ParameterSetName == "ByFilters" && string.IsNullOrEmpty(this.Host) && string.IsNullOrEmpty(this.User))
     {
         this.ThrowTerminatingError(VMware.VimAutomation.Sdk.Util10Ps.ErrorHandling.ExceptionHelper.VimExceptionToErrorRecord(VMware.VimAutomation.Sdk.Util10.ErrorHandling.ExceptionHelper.CreateClientSideException("Core_RemoveCredentialStoreItem_ProcessRecord_InvalidArguemnt", VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ErrorCategory.InvalidArgument, (object)null, (Exception)null, typeof(ViError), VMware.VimAutomation.ViCore.Cmdlets.ResourceHelper.ResourceService, (string)null)));
     }
     CredentialStoreItem[] credentialStoreItemArray;
     if (this.ParameterSetName == "ByCredentialItemObject")
     {
         credentialStoreItemArray = this._items;
     }
     else if (this.ParameterSetName == "ByFilters")
     {
         List <CredentialStoreItemImpl> list = new List <CredentialStoreItemImpl>();
         FileInfo         file            = string.IsNullOrEmpty(this.File) ? (FileInfo)null : new FileInfo(this.File);
         ICredentialStore credentialStore = (ICredentialStore)null;
         try
         {
             credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
             WildcardPattern wildcardPattern1 = new WildcardPattern("*", WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
             if (!string.IsNullOrEmpty(this.Host))
             {
                 wildcardPattern1 = new WildcardPattern(this.Host, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
             }
             WildcardPattern wildcardPattern2 = new WildcardPattern("*", WildcardOptions.Compiled);
             if (!string.IsNullOrEmpty(this.User))
             {
                 wildcardPattern2 = new WildcardPattern(this.User, WildcardOptions.Compiled);
             }
             foreach (string str1 in credentialStore.GetHosts())
             {
                 if (wildcardPattern1.IsMatch(str1))
                 {
                     foreach (string str2 in credentialStore.GetUsernames(str1))
                     {
                         if (wildcardPattern2.IsMatch(str2))
                         {
                             string password = new string(credentialStore.GetPassword(str1, str2));
                             list.Add(new CredentialStoreItemImpl(str1, str2, password, this.File));
                         }
                     }
                 }
             }
         }
         finally
         {
             if (credentialStore != null)
             {
                 credentialStore.Close();
             }
         }
         credentialStoreItemArray = (CredentialStoreItem[])list.ToArray();
     }
     else
     {
         credentialStoreItemArray = new CredentialStoreItem[0];
     }
     foreach (CredentialStoreItem credentialStoreItem in credentialStoreItemArray)
     {
         if (this.ShouldProcess(string.Format(VMware.VimAutomation.ViCore.Cmdlets.ResourceHelper.ResourceService.GetString("Core_RemoveCredentialStoreItem_ProcessRecord_Action"), (object)credentialStoreItem.Host, (object)credentialStoreItem.User)))
         {
             FileInfo         file            = string.IsNullOrEmpty(credentialStoreItem.File) ? (FileInfo)null : new FileInfo(credentialStoreItem.File);
             ICredentialStore credentialStore = (ICredentialStore)null;
             try
             {
                 credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
                 credentialStore.RemovePassword(credentialStoreItem.Host, credentialStoreItem.User);
             }
             finally
             {
                 if (credentialStore != null)
                 {
                     credentialStore.Close();
                 }
             }
         }
     }
 }
        /*Search Order for Username and Password
         * 1. Command Line Arguments
         * 2. Session File
         * 3. Enviroment Variable
         * 4. Credential Store
         * 5. Prompt the User for Username and Password
         */
        private void searchForUsernameAndPassword(Boolean flagUserName, Boolean flagPassword)
        {
            String username = null;
            String password = null;

            if (flagUserName && flagPassword)
            {
                return;
            }
            else if (optsEntered.ContainsKey("sessionfile"))
            {
                // Do Nothing
                return;
            }
            else
            {
                if (flagPassword == false)
                {
                    password = System.Environment.GetEnvironmentVariable("VI_Password");
                    if (password == null)
                    {
                        String host = null;
                        if (optsEntered.ContainsKey("server"))
                        {
                            foreach (DictionaryEntry uniEntry in optsEntered)
                            {
                                if (uniEntry.Key.Equals("server") && uniEntry.Value != null)
                                {
                                    host = uniEntry.Value.ToString();
                                }
                            }
                        }
                        else if (optsEntered.ContainsKey("url"))
                        {
                            String urlString = get_option("url");
                            foreach (DictionaryEntry uniEntry in optsEntered)
                            {
                                if (uniEntry.Key.Equals("url") && uniEntry.Value != null)
                                {
                                    urlString = uniEntry.Value.ToString();
                                }
                            }
                            if (urlString.IndexOf("https://") != -1)
                            {
                                int sind = 8;
                                int lind = urlString.IndexOf("/sdk");
                                host = urlString.Substring(sind, lind - 8);
                            }
                            else if (urlString.IndexOf("http://") != -1)
                            {
                                int sind = 7;
                                int lind = urlString.IndexOf("/sdk");
                                host = urlString.Substring(sind, lind - 7);
                            }
                            else
                            {
                                host = urlString;
                            }
                        }
                        else
                        {
                            // User Neds to Specify Either User Name aand Password
                        }
                        try
                        {
                            ICredentialStore csObj = CredentialStoreFactory.CreateCredentialStore();
                            //from here
                            int count = 0;
                            if (flagUserName == false)
                            {
                                username = System.Environment.GetEnvironmentVariable("VI_Username");
                                optsEntered["username"] = username;
                                if (username == null)
                                {
                                    IEnumerable userNameSet = csObj.GetUsernames(host);
                                    IEnumerator userNameIt  = userNameSet.GetEnumerator();

                                    while (userNameIt.MoveNext())
                                    {
                                        count++;
                                        username = userNameIt.Current.ToString();
                                        optsEntered["username"] = username;
                                        username = get_option("username");
                                    }
                                }
                                if (count > 1)
                                {
                                    username = readUserName();
                                    optsEntered["username"] = username;
                                    username = get_option("username");
                                }
                                flagUserName = true;
                            }
                            else
                            {
                                username = get_option("username");
                            }
                            // to here
                            char[] data = csObj.GetPassword(host, username);
                            password = new String(data);
                            optsEntered["password"] = password;
                        }
                        catch (Exception e)
                        {
                            // Do Noting
                            // Not able to find the password in Cred Store
                        }
                    }
                    else
                    {
                        optsEntered["password"] = password;
                    }
                    if (flagUserName == false)
                    {
                        username = System.Environment.GetEnvironmentVariable("VI_Username");
                        if (username == null)
                        {
                            // Read It From The Console
                            username = readUserName();
                        }
                        optsEntered["username"] = username;
                    }
                    if (password == null || password == "")
                    {
                        password = readPassword();
                        optsEntered["password"] = password;
                    }
                }
            }
        }