gpt4 book ai didi

powershell - 在PowerShell中,如何确定加入域的计算机是否连接到域网络?

转载 作者:行者123 更新时间:2023-12-03 01:07:25 27 4
gpt4 key购买 nike

在查询Active Directory之前,我需要确保计算机实际上已通过到域 Controller 的安全连接连接到公司网络(即域网络)。 Network Location Awareness在后台执行此操作,并在GUI中显示网络配置文件。

如何在PowerShell中执行此操作?

最佳答案

这似乎运行良好,并且应该在所有版本的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

如果返回$ true,则表明您已连接到域 Controller 。

关于powershell - 在PowerShell中,如何确定加入域的计算机是否连接到域网络?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com