简体   繁体   English

PowerShell:如何检查本地帐户是否已启用/活动

[英]PowerShell: How to check local accounts are Enabled/Active

I found this code here , and altered it to my needs.我在这里找到了这段代码,并根据我的需要进行了修改。 However one thing I need to add to the csv output is to indicate if the Local account is Enabled (or Active) or not.但是,我需要添加到 csv 输出中的一件事是指示本地帐户是否已启用(或活动)。 It would be nice to get it for the domain accounts as well, but the real need is for the Local machine accounts.为域帐户获取它会很好,但真正需要的是本地计算机帐户。

$strComputer = "domain-computer01.mydomain.com", "domain-computer02" 
$result = @()
foreach ($instance in $strcomputer)
{
    #Remove Domain from Hostname if it is used in $strComputer
    $instance_no_Domain = ($instance -replace ('(^[\w-_\d]+)\.(.*)', '$1'))
    Write-Host "Computer: $instance"
    $computer = [ADSI]"WinNT://$instance"
$objCount = ($computer.psbase.children | measure-object).count
    Write-Host "Q-ty objects for computer '$computer' = $objCount"
$Counter = 1
foreach ($adsiObj in $computer.psbase.children) {
    switch -regex($adsiObj.psbase.SchemaClassName) {
        "group" {
            $group = $adsiObj.name
            $LocalGroup = [ADSI]"WinNT://$instance/$group,group"
            $Members = @($LocalGroup.psbase.Invoke("Members"))
            $objCount = ($Members | measure-object).count
            Write-Host "Q-ty objects for group '$group' = $objCount"
            $GName = $group.tostring()

            ForEach ($Member In $Members) {
                $Name = $Member.GetType().InvokeMember("Name", "GetProperty", $Null, $Member, $Null)
                $Path = $Member.GetType().InvokeMember("ADsPath", "GetProperty", $Null, $Member, $Null)
                #$active =  $Member.GetType().InvokeMember("Enabled", "GetProperty", $Null, $Member, $Null)
                Write-Host " Object = $Path"
                    
                $isGroup = ($Member.GetType().InvokeMember("Class", "GetProperty", $Null, $Member, $Null) -eq "group")
                    If (($Path -like "*/$instance/*") -or ($Path -like "*/$instance_no_Domain/*") -Or ($Path -like "WinNT://NT*")) {
                    $Type = "Local"
                }
                Else { $Type = "Domain" }
                $result += New-Object PSObject -Property @{
                    Computername   = $instance_no_Domain
                    NameMember     = $Name
                    PathMember     = $Path
                    TypeMember    = $Type
                    ParentGroup    = $GName
                    isGroupMember = $isGroup
                #   IsActive    = $active
                    Depth          = $Counter
                }
            }
        }
    } #end switch
} #end foreach
} #end foreach instance
Write-Host "Total objects = " ($result | measure-object).count
$result = $result | select-object Computername, ParentGroup, NameMember, TypeMember, PathMember, isGroupMember, Depth #, IsActive
$result | Export-Csv -path ("C:\temp\LocalGroups({0})-{1:yyyyMMddHHmm}.csv" -f
    "list-of-accounts", (Get-Date)) -Delimiter "," -Encoding "UTF8" -force -NoTypeInformation

A simple option in PowerShell version 5.1 is to use Get-LocalUser PowerShell 5.1 版中的一个简单选项是使用Get-LocalUser

$user = Get-LocalUser -Name Administrator
$user.Enabled

You can also use Get-WmiObject or the newer version since PowerShell version 3 Get-CimInstance .您还可以使用Get-WmiObject或自 PowerShell 版本 3 Get-CimInstance以来的更新版本。

$user = Get-CimInstance -ClassName Win32_UserAccount -ComputerName <Server>
$user.Disabled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM