Пример #1
0
        public static string Normalize(string text, ObservableCollectionEx <Parameter> parameterList, Machine machine)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(string.Empty);
            }

            int normalizedCount = 0;

            string normalizedCommand = text;

            MatchCollection cmdVars = Regex.Matches(text, @"{%(.+?)%}", RegexOptions.IgnoreCase);

            if (cmdVars.Count > 0)
            {
                foreach (Match cmdVar in cmdVars)
                {
                    string cmdVarName = cmdVar.Groups[1].Value;

                    switch (cmdVarName)
                    {
                    case "MACHINE_NAME":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_NAME" : machine.Name);
                        normalizedCount++;
                        break;

                    case "MACHINE_IP":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_IP" : machine.IP);
                        normalizedCount++;
                        break;

                    case "MACHINE_USERNAME":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_USERNAME" : machine.Username);
                        normalizedCount++;
                        break;

                    case "MACHINE_PASSWORD":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_PASSWORD" : machine.Password);
                        normalizedCount++;
                        break;

                    case "MACHINE_DOMAIN":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_DOMAIN" : machine.Domain);
                        normalizedCount++;
                        break;

                    case "MACHINE_ISLINUX":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, machine == null ? "MACHINE_ISLINUX" : machine.IsLinux.ToString());
                        normalizedCount++;
                        break;

                    case "NOW":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss"));
                        normalizedCount++;
                        break;

                    case "ME":
                        normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                        normalizedCount++;
                        break;

                    default:
                        if (parameterList.Where(o => o.Name.Equals(cmdVarName)).Count() > 0)
                        {
                            string cmdVarValue = (parameterList.Where(o => o.Name.Equals(cmdVarName)).Single() as Parameter).Value;
                            normalizedCommand = Regex.Replace(normalizedCommand, cmdVar.Value, cmdVarValue);
                            normalizedCount++;
                        }
                        break;
                    }
                }

                if (normalizedCount > 0 && Regex.IsMatch(normalizedCommand, @"{%(.+?)%}", RegexOptions.IgnoreCase))
                {
                    normalizedCommand = Normalize(normalizedCommand, parameterList, machine);
                }
            }
            else
            {
                normalizedCommand = text;
            }
            return(normalizedCommand);
        }
Пример #2
0
 public static string Normalize(string text, ObservableCollectionEx <Parameter> parameterList)
 {
     return(Normalize(text, parameterList, null));
 }