mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
60
src/ServerManager.Common/Utils/MachineUtils.cs
Normal file
60
src/ServerManager.Common/Utils/MachineUtils.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using NLog;
|
||||
using System;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ServerManagerTool.Common.Utils
|
||||
{
|
||||
public static class MachineUtils
|
||||
{
|
||||
private const int OS_ANYSERVER = 29;
|
||||
|
||||
[DllImport("shlwapi.dll", SetLastError = true, EntryPoint = "#437")]
|
||||
private static extern bool IsOS(int os);
|
||||
|
||||
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public static bool IsWindowsServer()
|
||||
{
|
||||
return IsOS(OS_ANYSERVER);
|
||||
}
|
||||
|
||||
public static bool IsThisMachineADomainController()
|
||||
{
|
||||
try
|
||||
{
|
||||
Domain domain = Domain.GetCurrentDomain();
|
||||
if (domain == null)
|
||||
return false;
|
||||
|
||||
string thisMachine = $"{Environment.MachineName}.{domain}".ToLower();
|
||||
|
||||
var domainControllers = domain.DomainControllers.OfType<DomainController>();
|
||||
return domainControllers.Any(dc => dc.Name.Equals(thisMachine, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
catch (ActiveDirectoryObjectNotFoundException ex)
|
||||
{
|
||||
_logger.Debug($"{nameof(IsThisMachineADomainController)} checked. {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
catch (ActiveDirectoryOperationException ex)
|
||||
{
|
||||
_logger.Debug($"{nameof(IsThisMachineADomainController)} checked. {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error($"{nameof(IsThisMachineADomainController)}. {ex.Message}\r\n{ex.StackTrace}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsThisMachinePartOfADomain()
|
||||
{
|
||||
ManagementObject manObject = new ManagementObject(string.Format("Win32_ComputerSystem.Name='{0}'", Environment.MachineName));
|
||||
return (bool)manObject["PartOfDomain"];
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue