private ExecCmdResult ExecAppcmd(string arguments, ushort numTries = 1, TimeSpan?retrySleepInterval = null, bool expectError = false) { var execCmd = new ExecCmd(log, appCmdPath, arguments); return(execCmd.Run(numTries, retrySleepInterval, expectError)); }
/// <summary> /// NB: can block if error happens with "netsh" command. /// </summary> public bool ConfigureNetwork(string argIP, string argNetmask, string argGateway, IEnumerable <string> argDns) { IPAddress ip, netmask, gateway; var dnsAddrs = new List <IPAddress>(); try { ip = IPAddress.Parse(argIP); netmask = IPAddress.Parse(argNetmask); gateway = IPAddress.Parse(argGateway); if (false == argDns.IsNullOrEmpty()) { dnsAddrs.AddRange(argDns.Select(d => IPAddress.Parse(d))); } } catch (FormatException ex) { throw new ArgumentException("Invalid IP argument!", ex); } ConnectionInfo ci = GetFirstEthernetConnectionInfo(); string netshArgs = String.Format(NetshFmt, ci.NetConnectionID, ip.ToString(), netmask.ToString(), gateway.ToString()); bool success = false; var exec = new ExecCmd(log, "netsh", netshArgs); ExecCmdResult rslt = exec.Run(5, NetworkConfiguratorWaitInterval); success = rslt.Success; if (rslt.Success) { bool firstDns = true; foreach (var dnsAddress in dnsAddrs) { if (firstDns) { netshArgs = String.Format(NetshFirstDnsFmt, ci.NetConnectionID, dnsAddress.ToString()); firstDns = false; } else { netshArgs = String.Format(NetshRestDnsFmt, ci.NetConnectionID, dnsAddress.ToString()); } exec = new ExecCmd(log, "netsh", netshArgs); rslt = exec.Run(5, NetworkConfiguratorWaitInterval); success = rslt.Success; if (false == success) { break; } } } if (success) { this.NetworkConfigured = true; } return(success); }
private bool Sysprep() { bool wasSysprepped = false; VMSetupState vmSetupState = settings["vm-setup-state"].ToObject <VMSetupState>(); if (vmSetupState.IsSysprepped) { return(wasSysprepped); } string unattendXml = Resources.UnattendXML; var xdoc = XDocument.Parse(unattendXml); XNamespace ns = xdoc.Root.GetDefaultNamespace(); XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable()); nsMgr.AddNamespace("ns", ns.NamespaceName); // ComputerName var eleComputerName = xdoc.XPathSelectElement(@"/ns:unattend/ns:settings/ns:component/ns:ComputerName", nsMgr); string computerName = (string)settings["vm"]["name"]; eleComputerName.Value = computerName.Substring(0, Math.Min(15, computerName.Length)); // RegisteredOrganization var elements = xdoc.XPathSelectElements(@"//ns:component/ns:RegisteredOrganization", nsMgr); foreach (var ele in elements) { ele.Value = "Tier3"; // TODO } // RegisteredOwner elements = xdoc.XPathSelectElements(@"//ns:component/ns:RegisteredOwner", nsMgr); foreach (var ele in elements) { ele.Value = "Tier3"; // TODO } string pathToUnattend = Path.GetTempFileName(); using (var writer = XmlWriter.Create(pathToUnattend)) { xdoc.WriteTo(writer); } // TODO: this is the sysprep location on 2008 // http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1005593 var cmd = new ExecCmd(log, @"C:\Windows\System32\sysprep\sysprep.exe", "/quit /generalize /oobe /unattend:" + pathToUnattend); log.Info("Executing: '{0}'", cmd); ExecCmdResult rslt = cmd.Run(); log.Info("Result: '{0}'", rslt); if (rslt.Success) { wasSysprepped = vmSetupState.IsSysprepped = true; settings["vm-setup-state"] = JObject.FromObject(vmSetupState); SaveSettings(); } cmd = new ExecCmd(log, @"C:\Windows\System32\shutdown.exe", "/r /t 10 /c BOSHAgent /d p:4:2"); log.Info("Executing: '{0}'", cmd); rslt = cmd.Run(); log.Info("Result: '{0}'", rslt); return(wasSysprepped); }