public static void syncAsset(SnipeItApi snipe, SnipeSharp.Endpoints.Models.Asset currentAsset) { try { SnipeSharp.Endpoints.SearchFilters.SearchFilter assetFilter = new SnipeSharp.Endpoints.SearchFilters.SearchFilter() { Search = currentAsset.AssetTag }; SnipeSharp.Endpoints.Models.Asset foundAsset = snipe.AssetManager.FindOne(assetFilter); if (foundAsset != null && foundAsset.AssetTag == currentAsset.AssetTag) { Trace.WriteLine("Asset already exists in db. Not added."); } else { var response = snipe.AssetManager.Create(currentAsset); Trace.WriteLine("Response recieved from SnipeIT server after attempting to add asset: "); Trace.WriteLine(response); } } catch (Exception e) { Trace.WriteLine("Exception encountered while adding asset: " + e.ToString()); } }
static void Main(string[] args) { //TextWriterTraceListener listener = new TextWriterTraceListener(DateTime.Today.ToShortDateString() + "marksman.log"); // listener.Flush(); //Trace.Listeners.Add(listener); Trace.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + ": Started application."); var debugTimer = new Stopwatch(); System.Collections.Specialized.NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings; debugTimer.Start(); SnipeItApi snipe = new SnipeItApi(); snipe.ApiSettings.ApiToken = appSettings["API"]; snipe.ApiSettings.BaseUrl = new Uri(appSettings["BaseURI"]); // some test queries for analyzing duplicates SnipeSharp.Endpoints.SearchFilters.SearchFilter assetFilter = new SnipeSharp.Endpoints.SearchFilters.SearchFilter() { Search = "156TR4090-03" }; List <SnipeSharp.Endpoints.Models.Asset> snipeAssets = snipe.AssetManager.FindAll(assetFilter).Rows; Sentry mySentry = new Sentry(appSettings); // creating new Sentry (we can have multiple for parallel execution at a later point) /* CSystemType * Data type: uint16 * Access type: Read-only * * Qualifiers: MappingStrings ("") * * Type of the computer in use, such as laptop, desktop, or Tablet. * Under Win32_ComputerSystem class in WMI */ // Adding what we want mySentry.AddQuery("WMI", "SELECT Name, Manufacturer, Model, PCSystemType FROM Win32_ComputerSystem"); mySentry.AddQuery("WMI", "SELECT IdentifyingNumber FROM Win32_ComputerSystemProduct"); mySentry.AddQuery("WMI", "SELECT Name FROM Win32_BIOS"); mySentry.AddQuery("WMI", "SELECT Manufacturer,Name,MACAddress FROM Win32_NetworkAdapter WHERE NetEnabled=true AND AdapterTypeId=0 AND netConnectionStatus=2"); mySentry.AddQuery("WMI", "SELECT Manufacturer,Model,SerialNumber,Size FROM Win32_DiskDrive"); mySentry.AddQuery("WMI", "SELECT EndingAddress FROM Win32_MemoryArray"); mySentry.AddQuery("WMI", "SELECT Name FROM Win32_DesktopMonitor"); mySentry.AddQuery("WMI", "SELECT Manufacturer,Product,SerialNumber FROM Win32_BaseBoard"); mySentry.AddQuery("WMI", "SELECT Name,NumberOfCores,NumberOfLogicalProcessors FROM Win32_Processor"); bool getOU = false; bool getOUSuccess = Boolean.TryParse(appSettings["OUEnabled"], out getOU); if (getOUSuccess && getOU) { mySentry.AddQuery("Location", "OU"); } else { mySentry.AddQuery("Location", "Config"); } mySentry.Run(); SnipeSharp.Endpoints.Models.Asset currentComputer = mySentry.GetAsset(appSettings, snipe); Broker.syncAsset(snipe, currentComputer); debugTimer.Stop(); Trace.WriteLine("Total program execution time " + debugTimer.ElapsedMilliseconds + "ms."); Trace.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + ": Exiting application."); Trace.WriteLine(" "); }
public SnipeSharp.Endpoints.Models.Asset GetAsset(System.Collections.Specialized.NameValueCollection appSettings, SnipeItApi snipe) { string manufacturer = GetOutputVariable("Win32_ComputerSystem.Manufacturer"); string systemName = GetOutputVariable("Win32_ComputerSystem.Name"); string serialNumber = GetOutputVariable("Win32_ComputerSystemProduct.IdentifyingNumber"); string modelTotal = GetOutputVariable("Win32_ComputerSystem.Model"); string macAddress = GetOutputVariable("Win32_NetworkAdapter.MACAddress"); string systemType = GetOutputVariable("Win32_ComputerSystem.PCSystemType"); // TODO: Place in a separate enum class: // This enum should be in a separate class for enums Dictionary <string, string> PCSystemTypes = new Dictionary <string, string>(); PCSystemTypes.Add("0", "Undefined"); PCSystemTypes.Add("1", "Desktop"); PCSystemTypes.Add("2", "Laptop"); PCSystemTypes.Add("3", "Workstation"); PCSystemTypes.Add("4", "Enterprise Server"); PCSystemTypes.Add("5", "SOHO Server"); PCSystemTypes.Add("6", "Appliance PC"); PCSystemTypes.Add("7", "Performance Server"); PCSystemTypes.Add("8", "Maximum"); string systemTypeFull = "Undefined"; try { systemTypeFull = PCSystemTypes[systemType]; } catch (Exception e) { Trace.WriteLine("Exception encountered while processing PCSystemType: " + e.ToString()); } // TODO: This only works is in the exact format "ModelName ModelNumber" List <String> modelFragments = modelTotal.Split(' ').ToList(); string modelNumber = modelFragments[modelFragments.Count() - 1]; string modelMake = modelFragments[0]; SnipeSharp.Endpoints.SearchFilters.SearchFilter blankSearch = new SnipeSharp.Endpoints.SearchFilters.SearchFilter(); // should be from config file SnipeSharp.Endpoints.Models.Company currentCompany = new SnipeSharp.Endpoints.Models.Company { Name = appSettings["Company"] }; SnipeSharp.Endpoints.SearchFilters.SearchFilter companyFilter = new SnipeSharp.Endpoints.SearchFilters.SearchFilter() { Search = currentCompany.Name }; SnipeSharp.Endpoints.Models.Company searchedCompany = snipe.CompanyManager.FindOne(companyFilter); if (searchedCompany == null) { snipe.CompanyManager.Create(currentCompany); currentCompany = snipe.CompanyManager.FindOne(companyFilter); } else { currentCompany = searchedCompany; } string assetLocation = this.Values["Location"]; SnipeSharp.Endpoints.Models.Location currentLocation = new SnipeSharp.Endpoints.Models.Location { Name = assetLocation }; SnipeSharp.Endpoints.SearchFilters.SearchFilter locationFilter = new SnipeSharp.Endpoints.SearchFilters.SearchFilter() { Search = currentLocation.Name }; SnipeSharp.Endpoints.Models.Location searchedLocation = snipe.LocationManager.FindOne(locationFilter); if (searchedLocation == null) { var result = snipe.LocationManager.Create(currentLocation); currentLocation = snipe.LocationManager.FindOne(locationFilter); } else { currentLocation = searchedLocation; } SnipeSharp.Endpoints.Models.Model currentModel = new SnipeSharp.Endpoints.Models.Model { Name = modelTotal, Manufacturer = snipe.ManufacturerManager.Get(manufacturer), Category = snipe.CategoryManager.Get(systemTypeFull), ModelNumber = modelNumber, }; SnipeSharp.Endpoints.SearchFilters.SearchFilter modelFilter = new SnipeSharp.Endpoints.SearchFilters.SearchFilter() { Search = currentModel.Name }; SnipeSharp.Endpoints.Models.Model searchedModel = snipe.ModelManager.FindOne(modelFilter); if (searchedModel == null) { snipe.ModelManager.Create(currentModel); currentModel = snipe.ModelManager.FindOne(modelFilter); } else { currentModel = searchedModel; } Dictionary <string, string> customFields = new Dictionary <string, string>(); customFields.Add("_snipeit_macaddress_1", macAddress); bool isInteractive = false; bool interactiveParseSuccess = Boolean.TryParse(appSettings["Interactive"], out isInteractive); if (interactiveParseSuccess && isInteractive) { Console.WriteLine("Enter the computer name: "); systemName = Console.ReadLine(); } SnipeSharp.Endpoints.Models.StatusLabel currentStatusLabel = snipe.StatusLabelManager.Get("In Production"); string warrantyMonths = appSettings["WarrantyMonths"]; SnipeSharp.Endpoints.Models.Asset currentComputer = new SnipeSharp.Endpoints.Models.Asset { Company = currentCompany, AssetTag = appSettings["AssetTagPrefix"] + "-" + serialNumber, // <-- to be implemented.. somehow, somewhere Model = currentModel, StatusLabel = currentStatusLabel, RtdLocation = currentLocation, Name = systemName, Serial = serialNumber, WarrantyMonths = warrantyMonths, CustomFields = customFields, }; return(currentComputer); }