How do find out short name of domain I'm in?

泪湿孤枕 提交于 2019-12-24 04:07:10

问题


I want to return the short name of the domain the computer I am running on. I do not want the $env:USERDOMAIN because that is the domain the user is logged on to, which could be different to the domain the machine is in. If I do

(gwmi win32_computersystem).Domain

This gets me the FQDN, but I want something similar to return the short name (NETBIOS name?). I am currently parsing the FQDN, but I know this will lead to bugs later.

I don't have the Active Directory module installed, and company policy prevents me from installing it, so I can't simply use (Get-ADDomain).NetBIOSName from that module.


回答1:


OK, last try.

(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1'



回答2:


Bit late in the day for this but thought I'd offer my solution in case anyone else stumbles across this! If you just want the first part of the domain name then this code should work...

$domainName = ((gwmi Win32_ComputerSystem).Domain).Split(".")[0]



回答3:


As mentioned by Ben01635 this command works. The following will get you the first part of any domain. (e.g. Sub.company.com will translate to "Sub", if you wanted to get just the company part, you would change 0 to a 1, if you wanted com you would change 0 to a 3.)

(Get-WmiObject -Class win32_computersystem).domain.split(".")[0]

This command works by getting the Domaing using (Get-WmiObject -Class win32_computersystem).domain Then using the .split method to split that by the "." character. Then it selects the first part of the array that those strings are split into. This should always work to get the short path if you select 0 as it will always get the FIRST domain part that it encounters. Getting the middle Company part would not be as easy with variable domains but getting the first Short Domain form should be pretty easy. You can then go on to assign that to a Variable if you need to reference it in a script

$domainSearch = (Get-WmiObject -Class win32_computersystem).domain.split(".")[0]

You can do something similar with the OU of a computer if you are searching for Just the OU that a computer is in.

$searchOU = (([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().path).Split(",=")[3]


来源:https://stackoverflow.com/questions/21284114/how-do-find-out-short-name-of-domain-im-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!