如何查找计算机是否已加入特定机构的域与powershell命令的帮助

xoefb8l8  于 2023-11-18  发布在  Shell
关注(0)|答案(3)|浏览(172)

我想找出用户的计算机是否已加入域的particluar机构与powershell命令的帮助。
我尝试了下面的命令来检查域加入状态,但命令只显示域是否加入,但如果我想特别检查某个公司网络的域是否为“XXX”,该怎么办?

PS C:\windows\system32> (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain    
True

字符串

tcbh2hod

tcbh2hod1#

if ((gwmi win32_computersystem).partofdomain -eq $true) {
    write-host -fore green "I am domain joined!"
} else {
    write-host -fore red "Ooops, workgroup!"
}

字符串

6ljaweal

6ljaweal2#

你已经在正确的轨道上了,你只是查询了错误的属性。

(Get-CimInstance -ClassName Win32_Computersystem).Domain

字符串

vcirk6k6

vcirk6k63#

你可以从Win32_Computersystem类中获得更多信息。下面是一个帮助函数,你可以使用它获得这些信息:

function Get-ComputerInformation {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 0)]
        [string[]]$ComputerName = $env:COMPUTERNAME,

        [switch]$AsHashTable
    )

    foreach ($computer in $ComputerName) {

        # System info
        $data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer 

        $domainRole = switch ($data.DomainRole) {
            0 {'Standalone Workstation'; break}
            1 {'Member Workstation'; break}
            2 {'Standalone Server'; break}
            3 {'Member Server'; break}
            4 {'Backup Domain Controller'; break}
            5 {'Primary Domain Controller'}
        }

        $pcinfo = [ordered]@{}
        $pcinfo.'Computer'       = $data.Name
        $pcinfo.'Manufacturer'   = $data.Manufacturer
        $pcinfo.'Model'          = $data.Model
        $pcinfo.'PhysicalMemory' = "{0:N2} GB" -f ($data.TotalPhysicalMemory/1GB)
        $pcinfo.'PartOfDomain'   = $data.PartOfDomain
        if ($data.PartOfDomain) {
            $pcinfo.'Domain'     = $data.Domain
        }
        else {
            $pcinfo.'WorkGroup'  = $data.Workgroup
        }
        $pcinfo.'DomainRole'     = $domainRole
        $pcinfo.'LoggedOnUser'   = $data.Username

        if ($AsHashTable) { $pcinfo }
        else { [PsCustomObject]$pcinfo }
    }
}

字符串

相关问题