/// <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); }
public IDictionary <string, IList <int> > GetIIsWorkerProcesses() { IDictionary <string, IList <int> > rv = null; ExecCmdResult rslt = ExecAppcmd("list wp", 1, null, true); if (rslt.Success && false == rslt.Output.IsNullOrWhiteSpace()) { string[] lines = rslt.Output.Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (false == lines.IsNullOrEmpty()) { rv = new Dictionary <string, IList <int> >(); foreach (string line in lines) { Match m = workerProcessRegex.Match(line); int pid = Convert.ToInt32(m.Groups[1].Value); string apppoolName = m.Groups[2].Value; IList <int> pids; if (rv.TryGetValue(apppoolName, out pids)) { pids.Add(pid); } else { rv.Add(apppoolName, new List <int> { pid }); } } } } return(rv); }
private string GetIIsAppPoolState(string objectName) { string rv = null; ExecCmdResult rslt = ExecAppcmd(String.Format(@"list {0} ""/name:{1}""", IIsAppPoolObject, objectName), 1, null, true); if (rslt.Success) { Match m = apppoolStateRegex.Match(rslt.Output); rv = m.Groups[1].Value.ToLowerInvariant(); } return(rv); }
public WebServerAdministrationBinding InstallWebApp( string localDirectory, string applicationInstanceName, ushort managedRuntimeVersion) { WebServerAdministrationBinding rv = null; if (managedRuntimeVersion != 2 && managedRuntimeVersion != 4) { throw new ArgumentException( String.Format(Resources.WebServerAdministrationProvider_InvalidManagedRuntimeVersion_Fmt, managedRuntimeVersion), "managedRuntimeVersion"); } try { ushort applicationPort = 0; bool exists = DoesIIsAppPoolExist(applicationInstanceName); if (exists) { log.Error(Resources.WebServerAdministrationProvider_AppAlreadyExists_Fmt, applicationInstanceName); } else { // NB: must lock to ensure multiple threads don't grab the same port. lock (appcmdLock) { string cmd = String.Format("add apppool /name:{0}", applicationInstanceName); ExecCmdResult rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } cmd = String.Format( "set apppool {0} /autoStart:true /managedRuntimeVersion:v{1}.0 /managedPipelineMode:Integrated /processModel.loadUserProfile:true", applicationInstanceName, managedRuntimeVersion); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } applicationPort = Utility.RandomFreePort(); cmd = String.Format("add site /name:{0} /bindings:http/*:{1}: /physicalPath:{2}", applicationInstanceName, applicationPort, localDirectory); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } cmd = String.Format("set site {0} /[path='/'].applicationPool:{0}", applicationInstanceName); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } cmd = String.Format("set config {0} /section:system.webServer/httpLogging /dontLog:True", applicationInstanceName); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } cmd = String.Format("start apppool {0}", applicationInstanceName); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } cmd = String.Format("start site {0}", applicationInstanceName); rslt = ExecAppcmd(cmd, 5, twoSeconds); if (false == rslt.Success) { return(null); } } rv = new WebServerAdministrationBinding { Host = localIPAddress.ToString(), Port = applicationPort }; } firewallService.Open(applicationPort, applicationInstanceName); } catch (Exception ex) { log.Error(ex); } return(rv); }
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); }