Once a year our bosses (why is there always more than one?) ask us to produce reports outlining what workstations on campus need replaced.
Thanks to our campus logon script we are able to use WMI and retrieve the serial (along with a host of other attributes) for every workstation on campus. The problem was that we didn’t have matching warranty expiration dates. Unfortunately Dell does not have any sort of API for pulling warranty expiration dates by serial. Our solution was to create a console application that “screen scrapes” a dell warranty information page for shipping & warranty expiration dates.
The real meat and potato functions are fetchURL() and parseStringDell().
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
namespace SerialImporter
{
class Program
{
static void Main(string[] args)
{
getEmptySerials();
//Console.WriteLine("\r\nDone ... press any key to continue.");
//Console.ReadLine();
}
static void getEmptySerials()
{
// Query your database for a list of machine serials that you don't yet have warranty dates for.
//Run the following function for each serial you wish to retrieve
parseStringDell(fetchURL(serial), serial);
}
static string fetchURL(string Serial)
{
string URL = "http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&ServiceTag=" + Serial;
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(URL);
request.Timeout = 1000000;
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
return sb.ToString();
}
static void parseStringDell(string HTML, string Serial)
{
//Console.WriteLine("Working on: " + Serial);
DateTime warrantyEndDate = DateTime.Parse("1 Jan 1900");
DateTime shipDate = DateTime.Parse("1 Jan 2020");
DateTime tempDate;
string criteria = @"\d{1,2}/\d{1,2}/\d{4}";
MatchCollection m = Regex.Matches(HTML, criteria);
//Console.WriteLine(m.Count);
//Find the earliest and latest date, assume they are ship and warranty dates
for (int x = 0; x < m.Count; x++)
{
//Console.WriteLine(m[x].ToString());
tempDate = DateTime.Parse(m[x].ToString());
if (warrantyEndDate < tempDate)
warrantyEndDate = tempDate;
if (shipDate > tempDate)
shipDate = tempDate;
}
//Console.WriteLine("First Date: " + shipDate.ToString());
//Console.WriteLine("Last Date: " + warrantyEndDate.ToString());
// sometimes they don't have the ship date listed
if(shipDate==DateTime.Parse("1 Jan 2020"))
shipDate = DateTime.Parse("1 Jan 1900");
// get the model
string systemType = isolateString(HTML, "System Type:
","
");
updateDB(Serial, warrantyEndDate.ToString(), shipDate.ToString(), systemType);
}
private static string isolateString(string HTML, string searchFor1, string searchFor2)
{
int indexOne = HTML.IndexOf(searchFor1) + searchFor1.Length;
int indexTwo = HTML.IndexOf(searchFor2,indexOne);
//Console.WriteLine(indexOne + "-" + indexTwo);
string returnValue = HTML.Substring(indexOne,indexTwo-indexOne);
//Console.WriteLine(returnValue);
if (returnValue.Length <= 250)
return returnValue;
else
return null;
}
private static void updateDB(string serial, string warrantyDateTime, string shipDateTime, string systemType)
{
// Update your datebase
}
}
}
Let us know if you have any questions.