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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.DirectoryServices.AccountManagement;
using Excel = Microsoft.Office.Interop.Excel;
namespace ActiveDirectoryInformation
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private enum EunmDataGrid { empty, users, computers };
private EunmDataGrid EnumDataGrid1 = EunmDataGrid.empty;
private static string stringDomainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
private void buttonShow_Click(object sender, RoutedEventArgs e)
{
try
{
if (stringDomainName != null)
{
datagridResult.ItemsSource = null;
ShowComputers();
}
else
{
MessageBox.Show("Your computer is not a member of domain", "Active Directory Users", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void ShowComputers()
{
Computers1.Clear();
int intCounter = 0;
PrincipalContext PrincipalContext1 = new PrincipalContext(ContextType.Domain, stringDomainName);
ComputerPrincipal ComputerPrincipal1 = new ComputerPrincipal(PrincipalContext1);
PrincipalSearcher search = new PrincipalSearcher(ComputerPrincipal1);
foreach (ComputerPrincipal result in search.FindAll())
{
Computer Computer1 = new Computer(result.SamAccountName, result.DisplayName, result.Name, result.Description, result.Enabled, result.LastLogon);
Computers1.Add(Computer1);
intCounter++;
}
search.Dispose();
datagridResult.ItemsSource = Computers1;
datagridResult.Items.Refresh();
MessageBox.Show(intCounter + " computers. ");
EnumDataGrid1 = EunmDataGrid.computers;
}
private void buttonExport_Click(object sender, RoutedEventArgs e)
{
try
{
if (EnumDataGrid1 != EunmDataGrid.empty)
{
if (radiobuttonExcel.IsChecked == true)
{
Microsoft.Win32.SaveFileDialog SaveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
SaveFileDialog1.Filter = "Excel Workbook (*.xls)|*.xls";
if ((bool)SaveFileDialog1.ShowDialog())
{
if (EnumDataGrid1 == EunmDataGrid.computers)
{
ExportComputerstoExcel(SaveFileDialog1.FileName);
}
}
}
else
{
Microsoft.Win32.SaveFileDialog SaveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
SaveFileDialog1.Filter = "Comma-Seprated Value (*.csv)|*.csv";
if ((bool)SaveFileDialog1.ShowDialog())
{
if (EnumDataGrid1 == EunmDataGrid.computers)
{
ExportComputerstoCSV(SaveFileDialog1.FileName);
}
}
}
}
else
{
MessageBox.Show("First click on show button", "Active Directory Users", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ExportComputerstoExcel(string stringFileName)
{
Excel._Application ExcelApplication;
Excel.Workbook ExcelWorkbook;
Excel.Worksheet ExcelWorksheet;
object objectMisValue = System.Reflection.Missing.Value;
Excel.Range ExcelRangeCellinstance;
ExcelApplication = new Excel.Application();
ExcelWorkbook = ExcelApplication.Workbooks.Add(objectMisValue);
ExcelWorksheet = (Excel.Worksheet)ExcelWorkbook.Worksheets.get_Item(1);
ExcelApplication.DisplayAlerts = false;
ExcelRangeCellinstance = ExcelWorksheet.get_Range("A1", Type.Missing);
int intRow = 1;
int intColumn = 1;
foreach (string string1 in Computer.StringArrayComputerProperties)
{
ExcelWorksheet.Cells[intRow, intColumn] = string1;
intColumn++;
}
intRow++;
foreach (Computer Computer1 in Computers1)
{
intColumn = 1;
foreach (string string1 in Computer1.Properties())
{
ExcelWorksheet.Cells[intRow, intColumn] = string1;
intColumn++;
}
intRow++;
}
//Highlight first row
Excel.Range ExcelRange1 = ExcelWorksheet.get_Range("A1", Type.Missing);
ExcelRange1.EntireRow.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
ExcelRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightSkyBlue);
ExcelRange1.EntireRow.Font.Size = 14;
ExcelRange1.EntireRow.AutoFit();
//Save Excel
ExcelWorkbook.SaveAs(stringFileName, Excel.XlFileFormat.xlWorkbookNormal, objectMisValue, objectMisValue, objectMisValue, objectMisValue, Excel.XlSaveAsAccessMode.xlExclusive, objectMisValue, objectMisValue, objectMisValue, objectMisValue, objectMisValue);
ExcelWorkbook.Close();
MessageBox.Show("Saved Successfully", "Active Directory", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void ExportComputerstoCSV(string stringFileName)
{
// File FileStream1 = new System.IO.File();
StringBuilder StringBuilder1 = new StringBuilder(null);
foreach (string string1 in Computer.StringArrayComputerProperties)
{
if (StringBuilder1.Length == 0)
StringBuilder1.Append(string1);
StringBuilder1.Append(',' + string1);
}
StringBuilder1.AppendLine();
foreach (Computer Computer1 in Computers1)
{
StringBuilder StringBuilderTemp = new StringBuilder(null);
foreach (string string1 in Computer1.Properties())
{
if (StringBuilderTemp.Length == 0)
StringBuilderTemp.Append(string1);
StringBuilderTemp.Append(',' + string1);
}
// StringBuilder1.AppendLine();
StringBuilder1.AppendLine(StringBuilderTemp.ToString());
}
File.WriteAllText(stringFileName, StringBuilder1.ToString(), Encoding.UTF8);
MessageBox.Show("Saved Successfully", "Active Directory", MessageBoxButton.OK, MessageBoxImage.Information);
}
#region Computers
//public List<string> listStringUesrPropertie = new List<string> { "SamAccountName", "DisplayName", "Name", "GivenName", "Surname", "Description", "Enabled", "LastLogon" };
public Computers Computers1 = new Computers();
public class Computers : List<Computer> { }
public class Computer
{
public String SamAccountName { get; set; }
public String DisplayName { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public Boolean? Enabled { get; set; }
public DateTime? LastLogon { get; set; }
public Computer(String SamAccountName, String DisplayName, String Name, String Description,
Boolean? Enabled, DateTime? LastLogon)
{
this.SamAccountName = SamAccountName;
this.DisplayName = DisplayName;
this.Name = Name;
this.Description = Description;
this.Enabled = Enabled;
this.LastLogon = LastLogon;
}
public List<string> Properties()
{
return new List<string> { SamAccountName, DisplayName, Name, Description, Enabled.ToString(), LastLogon.ToString() };
}
public int UserPropertiesTotal = 6;
public static string[] StringArrayComputerProperties = { "SamAccountName", "DisplayName", "Name", "Description", "Enabled", "LastLogon" };
}
#endregion
private void windowMain_Loaded(object sender, RoutedEventArgs e)
{
try
{
string stringDomainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
if (stringDomainName != null)
{
PrincipalContext PrincipalContext1 = new PrincipalContext(ContextType.Domain, stringDomainName);
GroupPrincipal GroupPrincipal1 = new GroupPrincipal(PrincipalContext1);
PrincipalSearcher search = new PrincipalSearcher(GroupPrincipal1);
foreach (GroupPrincipal GroupPrincipal2 in search.FindAll())
{
}
}
else
{
MessageBox.Show("Your computer is not a member of domain", "Active Directory Users", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
} |