public static void Generate(string computerName, SyncList.SyncList <DprCurrentUsers> result) { Helpers.AssertNotNull(result, @"result SyncList cannot be null"); Helpers.AssertString(computerName, @"Computer name cannot be empty"); switch (GetNetworkUsers(computerName, ref result)) { case Win32.Error.Success: break; case Win32.Error.ErrorMoreData: break; case Win32.Error.ErrorAccessDenied: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"DprCurrentUsers - Generate - Access Denied for {0}", computerName); result.Add(new DprCurrentUsers(computerName, ConnectionStatuses.AccessDenied)); //return; break; default: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"DprCurrentUsers - Generate - Unknown Error for {0}", computerName); result.Add(new DprCurrentUsers(computerName, ConnectionStatuses.Error)); //return; break; } GetLocallyLoggedOnUsers(computerName, result); ValidateUniqueness(result); }
public static void ValidateUniqueness(SyncList.SyncList <DprCurrentUsers> rows) { var guids = new HashSet <Guid>( ); foreach (var item in rows) { Helpers.Assert(!guids.Contains(item.RowGuid), @"RowGuid's must be unique"); guids.Add(item.RowGuid); } }
public static void Generate(string computerName, SyncList.SyncList <DprComputerInfo> result) { Helpers.AssertNotNull(result, @"result SyncList cannot be null"); Helpers.AssertString(computerName, @"Computer name cannot be empty"); var ci = new DprComputerInfo(computerName) { LocalSystemDateTime = DateTime.Now }; try { WmiHelpers.ForEach(computerName, @"SELECT * FROM Win32_OperatingSystem WHERE Primary=TRUE", obj => { ci.LastBootTime = WmiHelpers.GetNullableDate(obj, @"LastBootUpTime"); ci.SystemTime = WmiHelpers.GetNullableDate(obj, @"LocalDateTime"); ci.Version = WmiHelpers.GetString(obj, @"Caption"); ci.Architecture = WmiHelpers.GetString(obj, @"OSArchitecture"); ci.InstallDate = WmiHelpers.GetNullableDate(obj, @"InstallDate"); return(true); }); WmiHelpers.ForEach(computerName, @"SELECT * FROM Win32_BIOS", obj => { ci.Manufacturer = WmiHelpers.GetString(obj, @"Manufacturer"); ci.HwReleaseDate = WmiHelpers.GetNullableDate(obj, @"ReleaseDate"); ci.SerialNumber = WmiHelpers.GetString(obj, @"SerialNumber"); ci.BiosVersion = WmiHelpers.GetString(obj, @"SMBIOSBIOSVersion"); return(true); }); WmiHelpers.ForEach(computerName, @"SELECT * FROM Win32_ComputerSystem", obj => { ci.Model = WmiHelpers.GetString(obj, @"Model"); ci.TotalPhysicalMemory = WmiHelpers.GetUInt(obj, @"TotalPhysicalMemory"); return(true); }); } catch (UnauthorizedAccessException uae) { GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"Exception - {0} - {1}", uae.TargetSite, uae.Message); ci.ConnectionStatus = ConnectionStatuses.AuthorizationError; } catch (Exception ex) { GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"Exception - {0} - {1}", ex.TargetSite, ex.Message); ci.ConnectionStatus = ConnectionStatuses.Error; } result.Add(ci); ValidateUniqueness(result); }
private static void GetLocallyLoggedOnUsers(string computerName, SyncList.SyncList <DprCurrentUsers> result) { var usersList = new List <DprCurrentUsers>( ); using (var regHku = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, string.Empty)) { foreach (var currentSid in regHku.GetSubKeyNames( ).Where(IsSid)) { var cu = new DprCurrentUsers(computerName) { Sid = currentSid }; try { if (Win32.WellKnownSids.ContainsKey(currentSid)) { cu.Domain = computerName; // Local account cu.UserName = Win32.WellKnownSids[currentSid]; } else { GetUserAccountFromSid(ref cu); } cu.ProfileFolder = RegistryHelpers.GetString(computerName, RegistryHive.LocalMachine, string.Format(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{0}", currentSid), @"ProfileImagePath"); cu.LastLogon = GetUsersLogonTimestamp(cu); } catch (Exception ex) { GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"Exception - {0} - {1}", ex.TargetSite, ex.Message); cu = new DprCurrentUsers(computerName, ConnectionStatuses.Error) { Sid = currentSid }; } cu.LogonType = LogonTypes.Local; usersList.Add(cu); } } result.AddRange(usersList); }
private static Win32.Error GetNetworkUsers(string computerName, ref SyncList.SyncList <DprCurrentUsers> result) { Win32.Error res; var er = 0; var tr = 0; var resume = 0; var buffer = IntPtr.Zero; var usersList = new List <DprCurrentUsers>( ); do { try { res = (Win32.Error)Win32.NetSessionEnum(computerName, null, null, 502, out buffer, -1, ref er, ref tr, ref resume); if (res == Win32.Error.ErrorMoreData || res == Win32.Error.Success) { var bufferPtrInt = buffer.ToInt32( ); for (var i = 0; i < er; i++) { var sessionInfo = (Win32.SessionInfo502)Marshal.PtrToStructure(new IntPtr(bufferPtrInt), typeof(Win32.SessionInfo502)); var cu = new DprCurrentUsers(computerName) { UserName = sessionInfo.userName, LastLogon = DateTime.Now.AddSeconds(-sessionInfo.logonDuration), LogonType = LogonTypes.Share }; usersList.Add(cu); bufferPtrInt += Marshal.SizeOf(typeof(Win32.SessionInfo502)); } } else { switch (res) { case Win32.Error.ErrorAccessDenied: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Access Denied: {0}", computerName); break; case Win32.Error.ErrorNotEnoughMemory: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Not Enough Memory: {0}", computerName); break; case Win32.Error.ErrorBadNetpath: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Bad Network Path: {0}", computerName); break; case Win32.Error.ErrorNetworkBusy: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Network Busy: {0}", computerName); break; case Win32.Error.ErrorInvalidParameter: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Parameter: {0}", computerName); break; case Win32.Error.ErrorInsufficientBuffer: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Insufficient Buff: {0}", computerName); break; case Win32.Error.ErrorInvalidLevel: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Level: {0}", computerName); break; case Win32.Error.ErrorExtendedError: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Exended Error: {0}", computerName); break; case Win32.Error.ErrorNoNetwork: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: No Network: {0}", computerName); break; case Win32.Error.ErrorInvalidHandleState: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Invalid Handle State: {0}", computerName); break; case Win32.Error.NerrBase: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: NERR_BASE: {0}", computerName); break; case Win32.Error.NerrUnknownDevDir: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Unknown Device Directory: {0}", computerName); break; case Win32.Error.NerrDuplicateShare: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Duplicate Share: {0}", computerName); break; case Win32.Error.NerrBufTooSmall: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: Buffer too small: {0}", computerName); break; case Win32.Error.ErrorNoBrowserServersFound: GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"GetNetworkUsers: No Browser Servers Found: {0}", computerName); break; } return(res); } } finally { if (IntPtr.Zero != buffer) { Win32.NetApiBufferFree(buffer); } } } while(res == Win32.Error.ErrorMoreData); result.AddRange(usersList); return(Win32.Error.Success); }