WinAdm.psm1

Function Get-ADLockedUser
{
    <#
    .SYNOPSIS
    Get the locked status of one or all users from Active Directory.
    .DESCRIPTION
    This function gets the locked status of one domain user or of all domain users from the Active Directory.
    .EXAMPLE
    Get-ADLockedUser
    SamAccountName Name
    -------------- ----
    SMAN Spider Man
    IMAN Iron Man
    UMAN Unknow Man
    .EXAMPLE
    Get-ADLockedUser -Logon JBOND
    UserPrincipalName LockedOut
    ----------------- ---------
    JBOND@yourdomain.net False
    .EXAMPLE
    Get-ADLockedUser -Logon JBOND -InformationLevel Detailed
    UserPrincipalName Name Enabled LockedOut
    ----------------- ---- ------- ---------
    JBOND@yourdomain.net James Bond True False
    .EXAMPLE
    Get-ADLockedUser -Logon JBOND -InformationLevel Quiet
    False
    .PARAMETER Logon
    The SamAccountName of some user.
    .PARAMETER InformationLevel
    You can use 'Detailed' to add or 'Quiet' to reduce the amount of information of this user.
    .LINK
    https://github.com/brunobritorj
    .NOTES
    Author: Bruno B Silva - brunobritorj@outlook.com
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)]
        [string]$Logon,

        [Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$false)]
        [ValidateSet('Detailed','Quiet')]
        [String]$InformationLevel
    )
    Process
    {
        If (!(Get-Module -ListAvailable -Name ActiveDirectory)) { Write-Error 'ActiveDirectory PowerShell module is required.' -Category NotInstalled }
        ElseIf ($Logon)
        {
            $User = Get-ADUser $Logon -Properties LockedOut
            Switch ($InformationLevel)
            {
                Detailed { Return $User | Select-Object UserPrincipalName, Name, Enabled, LockedOut}
                Quiet { Return $User.LockedOut }
                Default { Return $User | Select-Object UserPrincipalName, LockedOut }
            }
        }
        Else { Search-ADAccount -LockedOut | Select SamAccountName, Name }
    }
}
Function Get-ComputerLoggedUser
{
    <#
    .SYNOPSIS
    Get the logged user from a computer.
    .DESCRIPTION
    This function gets the logged user from a computer.
    .EXAMPLE
    Get-ComputerLoggedUser -ComputerName MACHINE01
    UserName
    --------
    DOMAINXYZ\JBOND
    .PARAMETER ComputerName
    The computer hostname that you would like to find out the logged user.
    .LINK
    https://github.com/brunobritorj
    .NOTES
    Author: Bruno B Silva - brunobritorj@outlook.com
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
        [string]$ComputerName
    )
    Process
    {
        $HostName = Resolve-DnsName $ComputerName -ErrorAction SilentlyContinue
        Switch ($HostName.Type)
        {
            A { $ComputerName = $HostName.Name }
            PTR { $ComputerName = $HostName.NameHost }
            default
            {
                Write-Error "$ComputerName not found" -Category ObjectNotFound
                Return
            }
        }
        If ($HostName)
        {
            Get-WmiObject â€“ComputerName $ComputerName â€“Class Win32_ComputerSystem | Select-Object UserName
        }
    }
}
Function Search-DeviceInDHCP
{
    <#
    .SYNOPSIS
    Search for a device lease in DHCP servers.
    .DESCRIPTION
    This function receives a device name and tries to find this host in leases from all DHCP servers from the domain. You can specify a DHCP server address to optimize the search.
    .EXAMPLE
    Search-DeviceInDHCP -DeviceName MACHINE01
    ServerIP HostName IPAddress ClientId
    -------- -------- --------- --------
    10.13.0.2 MACHINE01.dev.lan 10.13.153.25 40-fb-3b-bb-51-1a
    .EXAMPLE
    Search-DeviceInDHCP -DeviceName MACHINE02 -DHCPServers SRV01, SRV02, SRV03
    ServerIP HostName IPAddress ClientId
    -------- -------- --------- --------
    10.13.0.2 MACHINE02.dev.lan 10.13.178.14 40-fb-3b-bb-33-c2
    .PARAMETER DeviceName
    The name of the host that you are looking for.
    .PARAMETER DHCPServers
    The name or IP address of the DHCP servers that you would like to restrict the search.
    .LINK
    https://github.com/brunobritorj
    .NOTES
    Author: Bruno B Silva - brunobritorj@outlook.com
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$True)]
        [String]$DeviceName,
        
        [Parameter(Mandatory=$False,Position=1,ValueFromPipeline=$False)]
        [String[]]$DHCPServers = ((Get-DhcpServerInDC).DnsName)
    )
    Process
    {
        If (!(Get-Module -ListAvailable -Name DhcpServer)) { Write-Error 'DhcpServer PowerShell module is required.' -Category NotInstalled }
        Else
        {
            $ErrorActionPreference = 'SilentlyContinue'
            [int]$N = 1
            [int]$Total = $DHCPServers.count
            $Jobs = @()
            foreach ($SRV in $DHCPServers)
            {
                Write-Progress -Activity $SRV -Status ("$N/$Total Servers") -PercentComplete ($N/$Total*100)
                $Jobs += Get-DhcpServerv4Scope -ComputerName $SRV | Get-DhcpServerv4Lease -ComputerName $SRV -AsJob
                $N++
            }
            While ( (Get-Job -Id $Jobs.Id).State -eq 'Running' ) { Start-Sleep 5 }
            Get-Job -Id $Jobs.Id | Receive-Job | Where-Object Hostname -Match $DeviceName | Select-Object ServerIP, HostName, IPAddress, ClientId -Unique
            Remove-Job -Id $Jobs.Id
        }
    }
}