NetBIOS domain of computer in PowerShell

后端 未结 9 1871
一个人的身影
一个人的身影 2021-01-04 01:20

How can I get the NetBIOS (aka \'short\') domain name of the current computer from PowerShell?

$ENV:USERDOMAIN displays the domain of the current user, but I want th

9条回答
  •  孤独总比滥情好
    2021-01-04 01:34

    From Here

    # Retrieve Distinguished Name of current domain.
    $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $Root = $Domain.GetDirectoryEntry()
    $Base = ($Root.distinguishedName)
    
    # Use the NameTranslate object.
    $objTrans = New-Object -comObject "NameTranslate"
    $objNT = $objTrans.GetType()
    
    # Invoke the Init method to Initialize NameTranslate by locating
    # the Global Catalog. Note the constant 3 is ADS_NAME_INITTYPE_GC.
    $objNT.InvokeMember("Init", "InvokeMethod", $Null, $objTrans, (3, $Null))
    
    # Use the Set method to specify the Distinguished Name of the current domain.
    # Note the constant 1 is ADS_NAME_TYPE_1779.
    $objNT.InvokeMember("Set", "InvokeMethod", $Null, $objTrans, (1, "$Base"))
    
    # Use the Get method to retrieve the NetBIOS name of the current domain.
    # Note the constant 3 is ADS_NAME_TYPE_NT4.
    # The value retrieved includes a trailing backslash.
    $strDomain = $objNT.InvokeMember("Get", "InvokeMethod", $Null, $objTrans, 3)
    

提交回复
热议问题