2017-08-12 2 views
0

Avant d'interroger Active Directory, je dois m'assurer que l'ordinateur est réellement connecté au réseau de l'entreprise (c'est-à-dire, un réseau de domaine) avec une connexion sécurisée à un contrôleur de domaine. Network Awareness fait cela en coulisses et présente le profil du réseau dans l'interface graphique.Dans PowerShell, comment déterminer si un ordinateur connecté à un domaine est connecté au réseau de domaine?

Comment procéder dans PowerShell?

Répondre

1

Cela semble bien fonctionner et devrait fonctionner sur toutes les versions de PowerShell:

function Test-DomainNetworkConnection 
{ 
    # Returns $true if the computer is attached to a network where it has a secure connection 
    # to a domain controller 
    # 
    # Returns $false otherwise 

    # Get operating system major and minor version 
    $strOSVersion = (Get-WmiObject -Query "Select Version from Win32_OperatingSystem").Version 
    $arrStrOSVersion = $strOSVersion.Split(".") 
    $intOSMajorVersion = [UInt16]$arrStrOSVersion[0] 
    if ($arrStrOSVersion.Length -ge 2) 
    { 
     $intOSMinorVersion = [UInt16]$arrStrOSVersion[1] 
    } ` 
    else 
    { 
     $intOSMinorVersion = [UInt16]0 
    } 

    # Determine if attached to domain network 
    if (($intOSMajorVersion -gt 6) -or (($intOSMajorVersion -eq 6) -and ($intOSMinorVersion -gt 1))) 
    { 
     # Windows 8/Windows Server 2012 or Newer 
     # First, get all Network Connection Profiles, and filter it down to only those that are domain networks 
     $domainNetworks = Get-NetConnectionProfile | Where-Object {$_.NetworkCategory -eq "Domain"} 
    } ` 
    else 
    { 
     # Windows Vista, Windows Server 2008, Windows 7, or Windows Server 2008 R2 
     # (Untested on Windows XP/Windows Server 2003) 
     # Get-NetConnectionProfile is not available; need to access the Network List Manager COM object 
     # So, we use the Network List Manager COM object to get a list of all network connections 
     # Then we get the category of each network connection 
     # Categories: 0 = Public; 1 = Private; 2 = Domain; see: https://msdn.microsoft.com/en-us/library/windows/desktop/aa370800(v=vs.85).aspx 

     $domainNetworks = ([Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))).GetNetworkConnections() | ` 
      ForEach-Object {$_.GetNetwork().GetCategory()} | Where-Object {$_ -eq 2} 
    } 
    return ($domainNetworks -ne $null) 
} 

Avec cette fonction définie, tapez simplement:

Test-DomainNetworkConnection 

Si elle retourne $ true, alors vous savez vous avez une connectivité à un contrôleur de domaine.

+0

Quel genre de magie que cela accomplir: '[Activator] :: CreateInstance ([type] :: GetTypeFromCLSID ([Guid] "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")) '? – Vesper

+0

@Vesper, cet extrait de code crée une instance de l'objet COM Network List Manager. Le GUID en question est le GUID de l'objet COM NLM. –

0

Requêtes WMI pour savoir si l'ordinateur est sur le domaine. Si vrai, alors il va lister le domaine.

if ((gwmi win32_computersystem).partofdomain -eq $true) { 
    "DO WORK" 
} 

Ou requêtes WMI pour trouver le nom de domaine et si son connecté à ce domaine précis ne fonctionnent

if ((gwmi win32_computersystem).Domain -like "FAKE.DOMAIN.COM" -and (gwmi win32_computersystem).partofdomain -eq $true) { 
    "DO WORK" 
} 
+0

Cela ne me dit pas si l'ordinateur est sur le réseau de l'entreprise. Par exemple, je peux prendre un ordinateur portable à la maison et me connecter avec des informations d'identification en cache. Cet ordinateur portable serait connecté avec un compte de domaine et serait joint au domaine, mais pas sur le domaine/réseau d'entreprise –