C# - Get Service Tag & MAC address from a Dell laptop

This is a little script I wrote in C# to collect data about a Dell laptop and export it into a csv file. It collects the following information:
- Make
- Model
- Computer Name
- Wireless MAC Address
- Service Tag

If there is no csv file in the folder then the script will create one, if the file already exists then a row is appended to the existing file.

Source Code

This script has not been checked by Spiceworks. Please understand the risks before using it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net.NetworkInformation;usingSystem.Management;usingSystem.Text.RegularExpressions;usingSystem.IO;namespaceAsset_Tagging{classProgram{staticvoidMain(string[]args){stringcsvfile="assets.csv";stringcsvheader="MAKE,MODEL,COMPUTER_NAME,WIRELESS_MAC,SERVICE_TAG";stringcsvrow=",,,";stringversion="1.0";stringcomputername="UNKNOWN";stringservicetag="UNKNOWN";stringwifimac="UNKNOWN";stringmodel="UNKNOWN";stringmanufacturer="UNKNOWN";// Get Manufacturermanufacturer=GetManufacturer();// Get Modelmodel=GetModel();// Get computer namecomputername=GetComputerName();// Get the wifi MAC addresswifimac=GetWifiMAC();// Get the service tagservicetag=GetServiceTag();// Build csv rowcsvrow=manufacturer+","+model+","+computername+","+wifimac+","+servicetag;// Write data to fileif(!File.Exists(csvfile))File.WriteAllText(csvfile,csvheader+Environment.NewLine);File.AppendAllText(csvfile,csvrow+Environment.NewLine);// Write header to consoleConsole.WriteLine("*************************************");Console.WriteLine("ASSET TAGGING VERSION {0}",version);Console.WriteLine("*************************************");// Write info to consoleConsole.WriteLine("Make: {0}",manufacturer);Console.WriteLine("Model: {0}",model);Console.WriteLine("Computer Name: {0}",computername);Console.WriteLine("Wireless MAC: {0}",wifimac);Console.WriteLine("Service Tag: {0}",servicetag);// Wait for user to press a keyConsole.WriteLine();Console.WriteLine("Press any key to exit");Console.ReadKey();}staticprivatestringGetComputerName(){returnEnvironment.MachineName;}staticprivatestringGetWifiMAC(){stringMAC="UNKNOWN";foreach(NetworkInterfacenicinNetworkInterface.GetAllNetworkInterfaces()){if(nic.NetworkInterfaceType==NetworkInterfaceType.Wireless80211){MAC=nic.GetPhysicalAddress().ToString();// Insert hyphensMAC=String.Join("-",Regex.Matches(MAC,@"\w{2}").Cast<Match>());}}returnMAC;}staticprivatestringGetServiceTag(){stringservicetag="UNKNOWN";ManagementClasswmi=newManagementClass("Win32_Bios");foreach(ManagementObjectbiosinwmi.GetInstances()){servicetag=bios.Properties["Serialnumber"].Value.ToString().Trim();}returnservicetag;}staticprivatestringGetModel(){stringmodel="UNKNOWN";ManagementClasswmi=newManagementClass("Win32_ComputerSystem");foreach(ManagementObjectcomputerinwmi.GetInstances()){model=computer.Properties["Model"].Value.ToString().Trim();}returnmodel;}staticprivatestringGetManufacturer(){stringmanufacturer="UNKNOWN";ManagementClasswmi=newManagementClass("Win32_ComputerSystem");foreach(ManagementObjectcomputerinwmi.GetInstances()){manufacturer=computer.Properties["Manufacturer"].Value.ToString().Trim();}returnmanufacturer;}}}