2017-08-12 33 views

回答

1

這似乎很好地工作,並應在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) 
} 

通過這個函數,只需鍵入:

Test-DomainNetworkConnection 

如果返回$真,那麼你就知道您可以連接到域控制器。

+0

這是什麼樣的魔力確實做到:'[激活] ::的CreateInstance([類型] :: GetTypeFromCLSID([的Guid] 「{DCB00C01-570F-4A9B-8D69-199FDBA5723B}」))'? – Vesper

+0

@Vesper,該代碼片段創建網絡列表管理器COM對象的一個​​實例。有問題的GUID是NLM COM對象的GUID。 –

0

查詢WMI以查明計算機是否在域中。如果爲真,那麼它將列出域。

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

或者 查詢WMI查找域名,如果它連接到域完全做工作

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

這並不告訴我計算機是否在公司網絡上。例如,我可以將筆記本電腦帶回家並使用緩存的憑據登錄。這檯筆記本電腦將使用域帳戶登錄,並將加入域,但不在域/公司網絡上 –

相關問題