public static string MakeInvocationExpression(string filename, string function, IList <string> proc_params,
                                                      IList <string> extra_params, IList <IXenObject> objs, bool debug)
        {
            filename = Placeholders.Substitute(filename, objs);
            var expression = $"Invoke-Expression $(Get-Content -Path \"{filename}\" -Raw)";

            if (!string.IsNullOrEmpty(function))
            {
                function = Placeholders.Substitute(function, objs);
            }
            if (!string.IsNullOrEmpty(function))
            {
                expression = $"{expression}; {function}";
            }

            if (debug)
            {
                expression = string.Format(DebugFunction, expression);
            }

            string xenArrayStatement   = XenArrayStatement(proc_params);
            string extraArrayStatement = ExtraArrayStatement(extra_params, objs);

            return($"cd \"{Program.AssemblyDir}\"; {xenArrayStatement} {extraArrayStatement} {expression};");
        }
Beispiel #2
0
        public virtual Process CreateProcess(List <string> procParams, IList <IXenObject> targets)
        {
            Process proc = new Process();

            proc.StartInfo.FileName = Filename;
            // Targets can be null if the XenCenter node is being targetted, placeholders can cope with this
            proc.StartInfo.FileName = Placeholders.Substitute(Filename, targets);

            // 'Params' are defined in the plugin xml and may require substitution
            List <string> allParams = new List <string>(Params);

            for (int i = 0; i < allParams.Count; i++)
            {
                // sub in null, multi_target, or object properties depending on how many targets there are
                allParams[i] = Placeholders.Substitute(allParams[i], targets);
            }
            // 'procParams' come from ExternalPluginAction, and are tuples about each target (require no substitution)
            allParams.AddRange(procParams);
            proc.StartInfo.Arguments              = string.Join(" ", allParams.ToArray());
            proc.StartInfo.UseShellExecute        = !LogOutput;
            proc.StartInfo.CreateNoWindow         = !Window;
            proc.StartInfo.WindowStyle            = !Window ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal;
            proc.StartInfo.RedirectStandardOutput = LogOutput;
            proc.StartInfo.RedirectStandardError  = LogOutput;
            return(proc);
        }
Beispiel #3
0
        public static string MakeInvocationExpression(string filename, string function, IList <string> proc_params, IList <string> extra_params,
                                                      IList <IXenObject> objs, bool debug)
        {
            string expression = string.Format(InvokeExpression,
                                              Placeholders.Substitute(filename, objs),
                                              Placeholders.Substitute(function ?? "", objs));
            string xenArrayStatement   = XenArrayStatement(proc_params);
            string extraArrayStatement = ExtraArrayStatement(extra_params, objs);

            return(string.Format("cd \"{0}\"; {1} {2} {3};",
                                 Program.AssemblyDir, xenArrayStatement, extraArrayStatement,
                                 debug ? string.Format(DebugFunction, expression) : expression));
        }
        private static string ExtraArrayStatement(IList <string> extraParams, IList <IXenObject> objs)
        {
            // now we form a statement that will initialise a powershell array in the format (a,b,c,d,e,f,g)
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("${0}=@(", USER_PARAM_ARRAY_VAR_NAME));
            for (int i = 0; i < extraParams.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(',');
                }
                sb.Append('"');
                sb.Append(EscapeQuotes(EscapeBackSlashes(Placeholders.Substitute(extraParams[i], objs))));
                sb.Append('"');
            }
            sb.Append(");");

            return(sb.ToString());
        }