private Dictionary <string, string> DeleteUpdate(IUpdateServer wsus, string id) { Dictionary <string, string> dict = new Dictionary <string, string>(); try { IUpdate deletedUpdate = wsus.GetUpdate(new UpdateRevisionId(new Guid(id))); if (deletedUpdate.IsApproved) { deletedUpdate.Decline(); } wsus.DeleteUpdate(new Guid(id)); dict.Add("Title", deletedUpdate.Title); dict.Add("Id", deletedUpdate.Id.UpdateId.ToString()); dict.Add("Status", "Deleted"); } catch (WsusObjectNotFoundException) { dict.Add("Id", id); dict.Add("Status", "Not Found"); } return(dict); }
static void Main(string[] args) { var hook = new RemoteHook(); IUpdateServer wsusServer = ConnectLocal(); var dict = new Dictionary <string, string>(); if (args.Length > 0) { X509Certificate2 cert = DownloadSslCertificate(Environment.MachineName); switch (args[0]) { case "Set-DNSName": try { var name = args[1]; hook.SetDnsName(name); dict["value"] = "true"; hook.ReturnPayload(dict); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("ERROR: missing argument: <Name>"); } break; case "Get-DNSName": dict.Add("value", hook.GetDnsName()); hook.ReturnPayload(dict); break; case "Test-SSL": dict.Add("value", wsusServer.IsConnectionSecureForApiRemoting.ToString()); hook.ReturnPayload(dict); break; case "Set-Cert": try { var path = args[1]; var pass = args[2]; SetWsusCertificate(path, pass, wsusServer); dict["value"] = "true"; hook.ReturnPayload(dict); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("No Arguments found, generating new Certificate"); SetWsusCertificate(wsusServer); } break; case "Test-Conf": //Importing into other stores try { dict["value"] = "false"; X509Store store = new X509Store("WSUS", StoreLocation.LocalMachine); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); Console.WriteLine("Number of certificates: {0}{1}", fcollection.Count, Environment.NewLine); foreach (X509Certificate2 x509 in fcollection) { Console.WriteLine(x509); if (x509.GetNameInfo(X509NameType.DnsName, true) == "Carteiro") { dict["value"] = "true"; } } } catch (Exception) { dict["value"] = "false"; } hook.ReturnPayload(dict); break; case "Get-Cache": var cachePath = hook.GetCachePath(); List <Dictionary <string, string> > dataList = new List <Dictionary <string, string> >(); if (System.IO.Directory.Exists(cachePath)) { foreach (var file in System.IO.Directory.GetFiles(cachePath)) { Dictionary <string, string> entry = new Dictionary <string, string>(); entry.Add("MsiFileName", hook.GetMsiProperty(file, "ProductName")); entry.Add("PackageVersion", hook.GetMsiProperty(file, "ProductVersion")); dataList.Add(entry); } hook.ReturnPayload(dataList); } break; case "Test-Cert": if (cert != null) { if (cert.IssuerName.Name == hook.GetDnsName()) { dict["value"] = "true"; } else { dict["value"] = "false"; } } else { Console.WriteLine("ERROR: No Certificate found"); } break; case "Import-Package": try { var path = hook.GetCachePath() + args[1]; try { string desc = "Carteiro Update Package"; Console.WriteLine(path); string name = hook.GetMsiProperty(path, "ProductName"); string manufacturer = hook.GetMsiProperty(path, "Manufacturer"); string version = hook.GetMsiProperty(path, "ProductVersion"); IUpdate update = hook.ImportPackage(wsusServer, path, name, desc, manufacturer); dict.Add("Title", update.Title); dict.Add("Id", update.Id.UpdateId.ToString()); dict.Add("Manufacturer", manufacturer); dict.Add("Version", version); dict.Add("Status", "Imported"); dict.Add("CreationDate", update.CreationDate.ToString()); Console.WriteLine(dict.ToString()); } catch (Exception e) { dict.Add("Status", "Not Imported"); Console.Error.WriteLine(e); } hook.ReturnPayload(dict); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("ERROR: missing argument(s)"); } break; case "Get-Package": try { var path = args[1]; var name = args[2]; dict = hook.DownloadPackage(path, name); Console.WriteLine(dict["MsiFileName"]); dict.Add("Status", "Downloaded"); hook.ReturnPayload(dict); } catch (Exception e) { dict.Add("Value", "FileNotFound"); Console.Error.WriteLine(e); hook.ReturnPayload(dict); } break; case "Get-Updates": List <Dictionary <string, string> > resUpdates = new List <Dictionary <string, string> >(); resUpdates = args.Length > 1 ? hook.GetUpdates(wsusServer, args[1]) : hook.GetUpdates(wsusServer); hook.ReturnPayload(resUpdates); break; case "Delete-Update": try { var id = args[1]; dict = hook.DeleteUpdate(wsusServer, id); hook.ReturnPayload(dict); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("ERROR: missing argument(s)"); } catch (Exception e) { Console.Error.WriteLine(e); } break; //TODO Not Implemented Yet case "Approve-Update": try { var update_id = args[1]; var option = args[2]; var group_id = args[3]; Dictionary <string, string> resVal = new Dictionary <string, string>(); IUpdate update = wsusServer.GetUpdate(new UpdateRevisionId(new Guid(update_id))); IComputerTargetGroup group = wsusServer.GetComputerTargetGroup(new Guid(group_id)); switch (option) { case "Install": update.Approve(UpdateApprovalAction.Install, group); resVal.Add("Value", "installed"); break; case "Uninstall": if (update.UninstallationBehavior.IsSupported) { update.Approve(UpdateApprovalAction.Uninstall, group); resVal.Add("Value", "uninstalled"); } else { resVal.Add("Value", "not supported"); } break; case "NotApproved": try { update.Approve(UpdateApprovalAction.NotApproved, group); } catch (InvalidOperationException) { Console.Error.WriteLine("Cannot deapprove for all computers, instead declining it"); update.Decline(); resVal.Add("Value", "declined"); } resVal.Add("Value", "notApproved"); break; } update.RefreshUpdateApprovals(); hook.ReturnPayload(resVal); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("ERROR: missing argument(s)"); } break; case "Get-Groups": List <Dictionary <string, string> > resGroups = new List <Dictionary <string, string> >(); resGroups = hook.GetComputerTargetGroups(wsusServer); hook.ReturnPayload(resGroups); break; case "Get-Group": try { var id = args[1]; List <Dictionary <string, string> > resList = new List <Dictionary <string, string> >(); resList = hook.GetComputerTargetGroup(wsusServer, id); hook.ReturnPayload(resList); } catch (IndexOutOfRangeException) { Console.Error.WriteLine("ERROR: missing argument(s)"); } break; case "Get-Client-Status": try { var id = args[1]; List <Dictionary <string, string> > resList = new List <Dictionary <string, string> >(); Dictionary <string, string> retDict = new Dictionary <string, string>(); retDict = hook.GetComputerTargetStatus(wsusServer, id); resList.Add(retDict); hook.ReturnPayload(resList); } catch (Exception) { Console.Error.WriteLine("ERROR: missing argument(s)"); } break; default: Console.Error.WriteLine("ERROR: invalid operation"); Console.WriteLine("\nCarteiroWin Commands:\n"); Console.WriteLine("CarteiroWin Set-DNSName <Name> : Sets a new Registry Entry for the connection to local WSUS\n"); Console.WriteLine("CarteiroWin Get-DNSName : Get connectionstring for local WSUS\n"); Console.WriteLine("CarteiroWin Test-SSL : Test if ssl connection is possible\n"); Console.WriteLine("CarteiroWin Set-Cert [Path] [Password] : Import or generate certificate into WSUS Settings\n"); Console.WriteLine("CarteiroWin Test-Conf : Test Certificate Configuration\n"); Console.WriteLine("CarteiroWin Get-Cache : Returns the current Download Cache\n"); Console.WriteLine("CarteiroWin Get-Package <Path> <Name> : Imports local Path or URL to the CarteiroWin Cache\n"); Console.WriteLine("CarteiroWin Import-Package <Filename> : Imports File from CarteiroWin Cache into WSUS\n"); Console.WriteLine("CarteiroWin Get-Updates : Returns Local Published Updates from Carteiro Win\n"); Console.WriteLine("CarteiroWin Delete-Update <update_id> : Deletes the given Update\n"); Console.WriteLine("CarteiroWin Approve-Update <update_id> <'Install'|'Uninstall'|'NotApproved'> <targetgroup_id \n: Approves the Update with the given Rule for the given WSUS group\n"); Console.WriteLine("CarteiroWin Get-Groups : Returns all WSUS TargetGroups\n"); Console.WriteLine("CarteiroWin Get-Group <id> : Returns the members of the given Group Id\n"); Console.WriteLine("CarteiroWin Get-Client-Status <id> : Returns WSUS Report for a Client\n"); break; } //hook.ReturnPayload(dict); } else { Console.WriteLine("ERROR: no Operation given"); } }
private IUpdate ImportPackage(IUpdateServer wsus, string packagepath, string title, string desc, string vendor) { //Be sure that the baseapplicabilityrules.xsd exists var schemaPathx86 = Path.Combine("C:\\Program Files (x86)", "Update Services\\Schema"); var schemaPathx64 = Path.Combine("C:\\Program Files", "Update Services\\Schema"); var updateservicesPathx86 = Path.Combine("C:\\Program Files (x86)", "Update Services"); if (!File.Exists(schemaPathx86 + "baseapplicabilityrules.xsd")) { Console.WriteLine("INFO: Need to copy Update Services schemata into x86 Folder"); Console.WriteLine("DEBUG: " + schemaPathx86); Console.WriteLine("DEBUG: " + schemaPathx64); Console.WriteLine("DEBUG: " + updateservicesPathx86); if (!Directory.Exists(updateservicesPathx86)) { Directory.CreateDirectory(updateservicesPathx86); Console.WriteLine("INFO: Created Folder " + updateservicesPathx86); } if (!Directory.Exists(schemaPathx86)) { Directory.CreateDirectory(schemaPathx86); Console.WriteLine("INFO: Created Folder " + schemaPathx86); foreach (var file in Directory.GetFiles(schemaPathx64)) { File.Copy(file, Path.Combine(schemaPathx86, Path.GetFileName(file))); Console.WriteLine("INFO: Copy File " + file); } } } Console.WriteLine("Installing Package..."); SoftwareDistributionPackage sdp = new SoftwareDistributionPackage(); sdp.PopulatePackageFromWindowsInstaller(packagepath); sdp.Title = title; sdp.Description = desc; sdp.VendorName = vendor; //Look for Windows Vista sdp.IsInstallable = "<bar:WindowsVersion Comparison='GreaterThanOrEqualTo' MajorVersion='6' MinorVersion='0' />"; string sdpFilePath = Environment.GetEnvironmentVariable("TEMP") + "\\" + sdp.Title + sdp.PackageId.ToString() + ".txt"; //Superseed Update if there is one existing var searchString = title.Split(' ')[0]; foreach (IUpdate update in wsus.SearchUpdates(searchString)) { sdp.SupersededPackages.Add(update.Id.UpdateId); } sdp.Save(sdpFilePath); IPublisher publisher = wsus.GetPublisher(sdpFilePath); FileInfo dir = new FileInfo(packagepath); publisher.PublishPackage(dir.Directory.ToString(), null); Console.WriteLine("CAB generated"); IUpdate publishedUpdate = wsus.GetUpdate(new UpdateRevisionId(sdp.PackageId)); return(publishedUpdate); }