/// <summary> /// The do run set target group. /// </summary> /// <param name="server"> /// Object representing the WSUS server /// </param> /// <param name="targetGroup"> /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> /// <param name="alreadyProcessed"> /// List of target groups that have already been processed /// </param> private static void DoRunSetTargetGroup( IUpdateServer server, TargetGroup targetGroup, bool isTest, List <IComputerTargetGroup> alreadyProcessed) { Console.WriteLine("Processing Target Groups: " + targetGroup.Guid); // check the target group exists on wsus IComputerTargetGroup ctg = server.GetComputerTargetGroup(targetGroup.Guid); Console.WriteLine("Matched target group guid to: " + ctg.Name); // Get classifications if (targetGroup.Classifications.ElementInformation.IsPresent) { Console.WriteLine("Found a collection of specific classifications to apply."); DoRunSetClassifications(server, ctg, targetGroup.Classifications, isTest, alreadyProcessed); } else if (targetGroup.AllClassifications.ElementInformation.IsPresent) { Console.WriteLine("Applying all classifications."); DoRunSetAllClassifications(server, ctg, isTest, targetGroup.AllClassifications); } else { throw new ConfigurationErrorsException( "Unable to detect if we are running a specific set of classifications, or processing all classifications"); } }
/// <summary> /// Gets the "All Computers" group /// </summary> /// <param name="server"> /// Object representing the WSUS server /// </param> /// <returns></returns> public static IComputerTargetGroup GetRootTargetGroup(IUpdateServer server) { if (server == null) { throw new ArgumentNullException("server"); } return server.GetComputerTargetGroup(Microsoft.UpdateServices.Administration.ComputerTargetGroupId.AllComputers); }
/// <summary> /// Gets the "All Computers" group /// </summary> /// <param name="server"> /// Object representing the WSUS server /// </param> /// <returns></returns> public static IComputerTargetGroup GetRootTargetGroup(IUpdateServer server) { if (server == null) { throw new ArgumentNullException("server"); } return(server.GetComputerTargetGroup(Microsoft.UpdateServices.Administration.ComputerTargetGroupId.AllComputers)); }
/// <summary> /// The do run set all classifications. /// </summary> /// <param name="server"> /// The server. /// </param> /// <param name="ctg"> /// The ctg. /// </param> /// <param name="isTest"> /// The is Test. /// </param> /// <param name="allClassifications"> /// The all Classifications. /// </param> private static void DoRunSetAllClassifications( IUpdateServer server, IComputerTargetGroup ctg, bool isTest, Rule allClassifications) { // TODO: support products in all classifications element // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products = null; if (allClassifications.Copy) { // Check for copy Console.Out.WriteLine("Checking TargetGroup to Copy From: "); IComputerTargetGroup cftg = server.GetComputerTargetGroup(allClassifications.CopyFrom); Console.WriteLine("Matched copy from guid to: " + cftg.Name); CopyFrom.DoCopy(server, cftg, ctg, isTest); } if (allClassifications.ApproveSupersededUpdates) { // Check for superseded updates Console.Out.WriteLine("Checking Superseded Updates: "); CheckSupersededUpdates( server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest, allClassifications.ShouldApproveUninstalledSupersededUpdate); } if (allClassifications.ApproveStaleUpdates) { // Check for stale updates Console.Out.WriteLine("Checking Stale Updates: "); CheckStaleUpdates(server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest); } if (allClassifications.ApproveNeededUpdates) { CheckNeededUpdates(server, ctg, isTest); } }
private List <Dictionary <string, string> > GetComputerTargetGroup(IUpdateServer wsus, string id) { List <Dictionary <string, string> > retList = new List <Dictionary <string, string> >(); IComputerTargetGroup group = wsus.GetComputerTargetGroup(new Guid(id)); ComputerTargetCollection members = group.GetComputerTargets(); foreach (IComputerTarget member in members) { Dictionary <string, string> details = new Dictionary <string, string>(); details.Add("Id", member.Id); details.Add("Name", member.FullDomainName); details.Add("Ip", member.IPAddress.ToString()); details.Add("LastReport", member.LastReportedInventoryTime.ToString()); details.Add("ComputerRole", member.ComputerRole.ToString()); retList.Add(details); } return(retList); }
/// <summary> /// The do run set target group. /// </summary> /// <param name="server"> /// Object representing the WSUS server /// </param> /// <param name="targetGroup"> /// </param> /// <param name="isTest"> /// Whether we are in test mode /// </param> /// <param name="alreadyProcessed"> /// List of target groups that have already been processed /// </param> private static void DoRunSetTargetGroup( IUpdateServer server, TargetGroup targetGroup, bool isTest, List<IComputerTargetGroup> alreadyProcessed) { Console.WriteLine("Processing Target Groups: " + targetGroup.Guid); // check the target group exists on wsus IComputerTargetGroup ctg = server.GetComputerTargetGroup(targetGroup.Guid); Console.WriteLine("Matched target group guid to: " + ctg.Name); // Get classifications if (targetGroup.Classifications.ElementInformation.IsPresent) { Console.WriteLine("Found a collection of specific classifications to apply."); DoRunSetClassifications(server, ctg, targetGroup.Classifications, isTest, alreadyProcessed); } else if (targetGroup.AllClassifications.ElementInformation.IsPresent) { Console.WriteLine("Applying all classifications."); DoRunSetAllClassifications(server, ctg, isTest, targetGroup.AllClassifications); } else { throw new ConfigurationErrorsException( "Unable to detect if we are running a specific set of classifications, or processing all classifications"); } }
/// <summary> /// The do run set all classifications. /// </summary> /// <param name="server"> /// The server. /// </param> /// <param name="ctg"> /// The ctg. /// </param> /// <param name="isTest"> /// The is Test. /// </param> /// <param name="allClassifications"> /// The all Classifications. /// </param> private static void DoRunSetAllClassifications( IUpdateServer server, IComputerTargetGroup ctg, bool isTest, Rule allClassifications) { // TODO: support products in all classifications element // Microsoft.UpdateServices.Administration.UpdateCategoryCollection products = null; if (allClassifications.Copy) { // Check for copy Console.Out.WriteLine("Checking TargetGroup to Copy From: "); IComputerTargetGroup cftg = server.GetComputerTargetGroup(allClassifications.CopyFrom); Console.WriteLine("Matched copy from guid to: " + cftg.Name); CopyFrom.DoCopy(server, cftg, ctg, isTest); } if (allClassifications.ApproveSupersededUpdates) { // Check for superseded updates Console.Out.WriteLine("Checking Superseded Updates: "); CheckSupersededUpdates( server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest, allClassifications.ShouldApproveUninstalledSupersededUpdate); } if (allClassifications.ApproveStaleUpdates) { // Check for stale updates Console.Out.WriteLine("Checking Stale Updates: "); CheckStaleUpdates(server, allClassifications.AcceptLicenseAgreement, ctg, null, null, isTest); } if (allClassifications.ApproveNeededUpdates) { CheckNeededUpdates(server, ctg, isTest); } }
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"); } }