Uncategorized

Programming : C#

I have been itching to program in C# for a long time now.. finally got a chance to try it out due to requirement at work. Here’s a small snippet of code that does a bunch of things
1) Opens a directory and get a list of files in it
2) Parses the file name into multiple variables
3) Connects to Active Directory and gets properties for a user
4) Creates an error file with list of errors.
5) Creat an output file in csv format.

For the programming gurus out there, this should be pretty easy :).. I am sure that there are tons of errors in the way, I wrote the code, but this is a beginning :).


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.DirectoryServices;

namespace sortAssetData
{
class Program
{
static void Main(string[] args)
{
// Create a Log file and output file

System.IO.TextWriter infoLog = new System.IO.StreamWriter(“sortAssetDataLog.txt”);
System.IO.TextWriter outputLog = new System.IO.StreamWriter(“finalAssetData.csv”);

// Write the column headers into the output file
outputLog.WriteLine(“Last_Name, First_Name, Display_Name, Batt_Check_Version, Model_Type, Model_Number, Serial_Number, Batt_Manufacturer, Batt_Model, Batt_Serial_Number, Affected”);

// Define the folder where the data files are sitting
string fixDirectoy = “P:\\BatteryCheck_Results”;

// Change working directory to the folder with all the files
System.IO.Directory.SetCurrentDirectory(fixDirectoy);

// Create a directory object to get list of asset files
System.IO.DirectoryInfo tempDirObject = new System.IO.DirectoryInfo(fixDirectoy);

// Get list of *.csv files in the asset folder
System.IO.FileInfo[] listOfFiles = tempDirObject.GetFiles(“*.csv”);

// Process each file in the directory
foreach (System.IO.FileInfo tempFile in listOfFiles)
{
// Display the current file that is being processed
System.Console.WriteLine(“Processing : {0}”, tempFile.Name);
infoLog.WriteLine(“Processing : {0}”, tempFile.Name);
infoLog.Flush();

// Split the file name into Domain, Hostname, Username
string tempData = tempFile.Name;
// Removing the .csv at the end of the file name
tempData = tempData.Replace(“.csv”, “”);
string[] sortData = tempData.Split(new Char[] { ‘_’ });

// If file name doesn’t contain 3 records (Domain, ComputerName and UserName), we ignore it
if (sortData.Length != 3)
{
infoLog.WriteLine(“ERROR : Bad file format : {0}”, tempFile.Name);
infoLog.Flush();
continue;
}

// Display the userdata we got from the file name
System.Console.WriteLine(“\nDomain :\t {0}”, sortData[0]);
System.Console.WriteLine(“Computer Name :\t {0}”, sortData[1]);
System.Console.WriteLine(“User Name :\t {0}\n”, sortData[2]);

// Get the users full name from ActiveDirectory
System.DirectoryServices.DirectorySearcher searchUser = new System.DirectoryServices.DirectorySearcher();
searchUser.Filter = String.Format(“(SAMAccountName={0})”, sortData[2]);
searchUser.PropertiesToLoad.Add(“cn”);
searchUser.PropertiesToLoad.Add(“givenname”);
searchUser.PropertiesToLoad.Add(“sn”);
System.DirectoryServices.SearchResult resultFromAD = searchUser.FindOne();

// If the username doesn’t have a AD account, note and skip to the next file
if (resultFromAD == null)
{
System.Console.WriteLine(“ERROR : Bad User name : {0}”, sortData[2]);
infoLog.WriteLine(“ERROR : Bad User name : {0}”, sortData[2]);
infoLog.Flush();
continue;
}
else
{
//Parse the file and write data to the log file and also to the screen
System.IO.StreamReader tempSoureFile = new System.IO.StreamReader(tempFile.Name);
// Read the first line in the file and ignore it
string tempAssetData = tempSoureFile.ReadLine();
// Read the second line and store it to write to outputfile
tempAssetData = tempSoureFile.ReadLine();

string tempUserFirstName = resultFromAD.Properties[“sn”][0].ToString();
string tempUserLastName = resultFromAD.Properties[“givenname”][0].ToString();
string tempUserDisplayName = resultFromAD.Properties[“cn”][0].ToString();
outputLog.WriteLine(“{0},{1},{2},{3}”, tempUserFirstName, tempUserLastName, tempUserDisplayName, tempAssetData);
outputLog.Flush();
System.Console.WriteLine(“User First Name:\t {0}”, tempUserFirstName);
System.Console.WriteLine(“User Last Name:\t\t {0}”, tempUserLastName);
System.Console.WriteLine(“Display Name:\t\t {0}”, tempUserDisplayName);
}
}
System.Console.ReadKey();
infoLog.Close();
outputLog.Close();
}
}
}


Microsoft Excel : How to repair "Opening blank file when double clicking on an excel file"

Ran into an interesting problem with troubleshooting an Excel issue. Double clicking on an excel file opened a blank file on the affected machine. I thought it might be something related to the associations and checked them from “Tools -> Folder Options -> File Types” in the Windows Explorer menu. Everything was in order there. I then tried a reinstall of Office (note : would not do this in the future without additional troubleshooting 🙂 ). That didn’t help either. Finally, I gave up and did a Google search, which returned this URL

http://groups.google.com/group/microsoft.public.office.misc/browse_thread/thread/aa3da7b29cd55a93/3347ca4c2ea6673a%233347ca4c2ea6673a

Did the following according to the post and everything returned to default settings

1) Run Excel with “/safe” option.
2) Click on “help -> Detect and Repair” from the Excel menu
3) Close Excel for repair

Thx Google.

New cuisine..

We went to Ros Dashen, an Ethiopian restaurant to celebrate a friends upcoming birthday. For those that haven’t tried Ethiopian food and like spicy cuisine, I would highly recommend trying it. In addition to the tasty food, the whole experience of sharing the same plate is very interesting. The food is served on top of pancacke like sour bread that you end tearing up and dipping/wiping the food in along with the rest of the folks at the table. Here are two pictures that show, how much we appreciated the food..

This is what we got…

This is what it ended up as… 🙂

HOW TO : Blackberry : How to use it as a modem

On popular demand..A lot of people asked me how you can use the BB as a modem, the way I specified in this post. Here’s the link with detailed instructions on how to do this..

http://www.blackberryforums.com/blackberry-guides/2019-user-howto-use-blackberry-modem-laptop-11.html

Note: Make sure you have an unlimited data plan from your service provider or you can get dinged with a large bill :).

Yet another site powered by the masses

www.soapboxit.com is a website where anyone can write/search reviews about anything. I came across it when I learnt that one of my old colleagues is going (looks like I got the wrong company. The company he is going to work at is www.soapboxreviews.com) to work at this company. I think it is an interesting idea. As with all social networks, the challenge will be to gain momentum.. As I was browsing the site, I noticed that each new user is given a ID. At this point, the ID seems to just be an increasing integer. I found that my ID is 212 🙂

http://soapboxit.com/person/212

RDP over SSH tunnel

Necessity is the mother of invention.. Or so goes the old saying. I was on-call this weekend for off hour IT support. We usually provide our on-call engineers a wireless broadband card in case they need to get connected to the corporate network to troubleshoot/fix issues. This week, I was not able to get the wireless card, since we had to loan it to an exec. The only method to get onto the Internet was using my new 8700 blackberry. We have the EDGE service from T-mobile, which is not as fast as the Verizon BroadbandAccess.. We typically get ~128kbps with the EDGE connection and is enough to get our work done.. The problem with using the BB as a modem is that T-mobile blocks PPTP/IPSec traffic with this plan. Not to be outdone, I came up with a way to use the BB to connect to our internal network. Most of our support work is done on Windows, so we need to remote console into one of our servers to provide support. I did the following to get this working using our corporate SSH gateway to tunnel RDP traffic.

1) Download Putty (SSH Client).
2) Create a new session on Putty to the SSH gateway. In the “Tunnels” option, add the following options

Note: Replace the REMOTE_HOST with the IP address or hostname of your Windows server.
3) Click on ADD
4) Click on Open and connect to the SSH server with your credentials
5) Log into the remote serve by running the following command “mstsc /v:127.0.01:3390
Note: You can change the local port to any number you want. Make sure not to use 3389 as the local port, since it is already used by the remote desktop service on your windows workstation.

Labour day weekend : Good Beer, Good Friends.. Good times :)

It’s amazing how fast holidays zip by :). Wish, it was the same with work days…. Lakshmi and I didn’t get a chance to travel during this long weekend, but we caught up with some sightseeing in Chicago. We visited the Field museum, using the free family pass you can get at the Chicago Public Library. For those of you who are not aware, you can get free passes to most of the attractions in Chicago. All you need is a membership at the library. Each library has limited quantities of the passes and it is given out on a first come first serve basis. A great way to explore the city without breaking your bank :). It was great day to visit the museum. I took some snaps from the trip and posted them on Flickr

We also got a chance to catch up with old friends..Sudheer and his charming wife, Neelima flew down to Chicago for the weekend and all of us got together at Ravi’s place for a night of fun, beer and food :). It was great to catch up on life and fondly remember our days at school. As usual, me and my camera were busy snapping pictures of the night…


[Left to Right: Back Row: Shekar, Achi, Ravi, Byna, Sudheer Front Row: Nirmala, Pallavi, Madhavi, Deepthi, Lakshmi, Neelima]