public void init() { XmlDocument doc = new XmlDocument(); if (File.Exists(externalFile)) { doc.Load(externalFile); } else { var assembly = Assembly.GetExecutingAssembly(); var templstream = assembly.GetManifestResourceStream("SuperEdit.Templates.xml"); doc.Load(templstream); } XmlNodeList nodes = doc.DocumentElement.SelectNodes("//objectTemplate"); foreach (XmlNode node in nodes) { var objt = new ObjectTemplate(); objt.Name = node.Attributes["name"].Value; var filternode = node.SelectSingleNode("objectfilter"); var templates = node.SelectNodes("template"); //Console.WriteLine("Node " + objt.Name); //Console.WriteLine("Filter " + filternode); //Console.WriteLine("TemplC " + templates.Count); if (filternode != null && templates.Count > 0) { objt.Filter = filternode.InnerText.Trim(); objt.FilterSelect = filternode.Attributes["select"].Value; //Console.WriteLine("Filter Select" + objt.FilterSelect); //Console.WriteLine("Filter txt" + objt.Filter); foreach (XmlNode t in templates) { var templ = new Template(); templ.Name = t.Attributes["name"].Value; templ.Script = t.SelectSingleNode("script").InnerText.Trim(); var pres = t.SelectSingleNode("prescript"); if (pres != null) { templ.PreScript = pres.InnerText.Trim(); } var post = t.SelectSingleNode("postscript"); if (post != null) { templ.PostScript = post.InnerText.Trim(); } var vals = t.SelectNodes("values/value"); var dynval = t.SelectSingleNode("dynamicvalue"); if (vals.Count > 0) { foreach (XmlNode v in vals) { var real = ""; if (v.Attributes["real"] != null) { real = v.Attributes["real"].Value; } var val = new Value(v.InnerText.Trim(), real); templ.Values.Add(val); } objt.Templates.Add(templ); } else if (dynval != null) { //undocumented //pipeline should return objects with Display (+Real if required) // | select @{N='Display';E={$_.DisplayName}},@{N="Real";E={$_.Name}} templ.DynValScript = dynval.InnerText.Trim(); objt.Templates.Add(templ); } } global.Add(objt); } } }
public string BuildStack(ObjectTemplate ot, Template t, string valReal, BindingList <SelectObject> objects) { var sb = new StringBuilder(1024); sb.AppendLine("$verbosepreference='continue';function progvb($prog) { write-verbose (\"PROG{0,4}PCT\" -f $prog); }"); sb.AppendLine("progvb 5"); sb.AppendLine("Add-PSSnapin VeeamPSSnapIn -ErrorAction SilentlyContinue"); sb.AppendLine("progvb 10"); sb.AppendLine(""); sb.AppendLine("$all = " + ot.Filter); sb.AppendLine("$value = " + valReal); sb.AppendLine("progvb 20"); sb.AppendLine(""); sb.AppendLine(""); if (t.PreScript != "") { sb.AppendLine(t.PreScript); sb.AppendLine(""); } sb.AppendLine("progvb 30"); int sels = 0; foreach (var obj in objects) { if (obj.selected) { sels++; } } double stepsize = 58.0 / sels; double cstep = 30; double prevemit = cstep; double emitinterval = 5; foreach (var obj in objects) { if (obj.selected) { sb.AppendLine("$obj = $all | ? { $_." + ot.FilterSelect + " -eq \"" + obj.name + "\" }"); sb.AppendLine(t.Script); if ((cstep - emitinterval) >= prevemit) { sb.AppendLine("progvb " + Math.Round(cstep)); prevemit = cstep; } cstep += stepsize; sb.AppendLine(""); } } sb.AppendLine("progvb 90"); if (t.PostScript != "") { sb.AppendLine(t.PostScript); sb.AppendLine(""); } sb.AppendLine("progvb 100"); return(sb.ToString()); }