src/windows/public/Get-ADComputerRange.ps1

# _____________________________________________________
# | | #
# | Get-ADComputerRange.ps1 | #
# |_____________________________________________________| #


function Get-ADComputerRange() {


    # =================================== #
    # PARAMETERS (call) #
    #
    # DESCRIPTION : calling parameter(s) of the script.

    [CmdletBinding()]
    Param (
        [string]$PATH,
        [Parameter(Mandatory=$True)]
        [string]$COMPUTER,
        [bool]$USER,
        [string]$RANGE,
        [bool]$QUIET
    )


    <#
        .Synopsis
            Show AD Computer state (name free, offline or online) by range and for those that are in use, displays the user logged in. Result exported to a CSV file.

        .Example
            Get-ADComputerRange -PATH "C:\GLOBAL\" -COMPUTER "GLOBAL" -RANGE "0005-0015" -USER $False

        .Parameter PATH
            Folder receive the result into a CSV file. | default is 'C:\GADCR\'.

        .Parameter COMPUTER
            Name of device. | no default.

        .Parameter USER
            Display current user logged if computer is online. | default is '$True'.

        .Parameter RANGE
            Pool of device to check. | default is '0' to '100'.

        .Parameter QUIET
            Echo or not | default is '$False'.

    #>



    # ============================= #
    # APPLICATION #
    #
    # DESCRIPTION : everything that defines the APP.

    $APP = @{};
    $APP["NAME"] = "Get-ADComputerRange";
    $APP["SHORTNAME"] = "GADCR";
    $APP["DESCRIPTION"] = "Show AD Computer state (name free, offline or online) by range and for those that are in use, displays the user logged in. Result exported to a CSV file.";
    $APP["VERSION"] = "1.0.0";
    $APP["AUTHOR"] = "Frédéric Petit <contact@fredericpetit.fr>";
    $APP["LICENSE"] = "MIT";


    # =============================== #
    # CONFIGURATION #
    #
    # DESCRIPTION : everything that configures the APP.

    $CONF = @{};
    $CONF["DATE"] = Get-Date -Format "yyyyMMdd_HHmm";
    $CONF["PATH"] = "$PSScriptRoot";


    # ======================================== #
    # PARAMETERS (variables) #
    #
    # DESCRIPTION : set an array of calling parameter(s) of the script and take default(s) value(s) if needed.

    $PARAM = @{};
    [string]$PARAM["PATH"] = $PATH;
    [string]$PARAM["COMPUTER"] = $COMPUTER;
    [bool]$PARAM["USER"] = $USER;
    [string]$PARAM["RANGE"] = $RANGE;
    $DEFAULT = @{};
    [string]$DEFAULT["PATH"] = "C:\$($APP.SHORTNAME)";
    [bool]$DEFAULT["USER"] = $True;
    [string]$DEFAULT["RANGE"] = "0-100";
    [bool]$DEFAULT["QUIET"] = $False;
    If (!$PARAM["PATH"]) { [string]$PARAM["PATH"] = $DEFAULT["PATH"]; }
    If (!$PARAM["COMPUTER"]) { Stop-App 1; }
    If (!$PARAM["USER"]) { [string]$PARAM["USER"] = $DEFAULT["USER"]; }
    If ($PARAM["RANGE"] -clike "*-*") {
        [string]$PARAM["START"] = $($PARAM.RANGE).Split("-")[0];
        [string]$PARAM["END"] = $($PARAM.RANGE).Split("-")[1];
    } ElseIf ($PARAM["RANGE"] -clike "*.*") {
        [string]$PARAM["START"] = $($PARAM.RANGE).Split(".")[0];
        [string]$PARAM["END"] = $($PARAM.RANGE).Split(".")[1];
    } Else {
        If ($PARAM["RANGE"]) { Stop-App 1; }
    }
    If (!$PARAM["START"]) { [string]$PARAM["START"] = $($DEFAULT.RANGE).Split("-")[0]; }
    If (!$PARAM["END"]) { [string]$PARAM["END"] = $($DEFAULT.RANGE).Split("-")[1]; }
    If (!$PARAM["QUIET"]) { [string]$PARAM["QUIET"] = $DEFAULT["QUIET"]; }


    # =========================== #
    # FUNCTIONS #

    # Test-Computer()
    # DESCRIPTION : check computer existence and connectivity.
    # RETURN : string.
    function Test-Computer([int]$INDEX,[string]$NAME) {
        # Test : existence of the machine in Active Directory.
        If (Get-ADComputer -Filter "Name -eq '$NAME'") {
            # Calculates the number of days since the last login.
            $LASTLOGON = Get-ADComputer -Filter "Name -eq '$NAME'" -Properties LastLogon | Select-Object @{N='LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}}, @{N='Last Logon Days'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}} | Select-Object -ExpandProperty "Last Logon Days";
            # Test : the difference in last connection brings back to "Win32 epoch January 1, 1601", verification is necessary on the computer account.
            If ($LASTLOGON -gt 150000) { $LASTLOGON = "need check"; }
            # Test : connectivity.
            If (Test-Connection $NAME -Count 2 -Delay 2 -Quiet) {
                # Test : respect privacy.
                If ($($PARAM.USER) -eq $True) {
                    $USERLOGGED = Get-UserLogged "$NAME";
                    $FILE = "$NAME;online;$LASTLOGON;$USERLOGGED";
                    If ($($PARAM.QUIET) -eq $False) { Write-Host "#$(Convert-TextWithSpace -TEXT $INDEX -PAD 4) ${NAME} online$(" " * $($COUNT_STATE - 6)) $(" " * $($COUNT_LASTLOGON)) ${USERLOGGED}" -ForegroundColor DarkRed; }
                } Else {
                    $FILE = "$NAME;online;$LASTLOGON;private";
                    If ($($PARAM.QUIET) -eq $False) { Write-Host "#$(Convert-TextWithSpace -TEXT $INDEX -PAD 4) ${NAME} online" -ForegroundColor DarkRed; }
                }
            } Else {
                $FILE = "$NAME;offline;$LASTLOGON;";
                If ($($PARAM.QUIET) -eq $False) { Write-Host "#$(Convert-TextWithSpace -TEXT $INDEX -PAD 4) ${NAME} offline$(" " * $($COUNT_STATE - 7)) ${LASTLOGON}" -ForegroundColor Yellow; }
            }
        } Else {
            $FILE = "$NAME;free;never;";
            If ($($PARAM.QUIET) -eq $False) { Write-Host "#$(Convert-TextWithSpace -TEXT $INDEX -PAD 4) $(Convert-TextWithSpace -TEXT $NAME -PAD $($COUNT_NAME + 4)) free" -ForegroundColor DarkGreen; }
        }
        $FILE | Add-Content -Path "$($PARAM.PATH)\$($PARAM.COMPUTER)-$($CONF.DATE).csv";
    }


    # ====================== #
    # CALL #
    #
    # DESCRIPTION : starting calls by the APP.

    # Test : load AD.
    $VERIFYAD = $(Verify-Software -NAME "ActiveDirectory" -TYPE "module" -ACTION "load" -DEPEND $True);
    If ($VERIFYAD -clike "fail-*") { Write-Warning "module Active Directory can't be loaded ('${VERIFYAD}')."; Break; }
    # CSV file init.
    $FILE = "";
    Test-File -PATH "$($PARAM.PATH)\$($PARAM.COMPUTER)-$($CONF.DATE).csv" -CREATE $True -CONTENT "Name;State;LastLogon;User" -OVERWRITE $True -QUIET $True;
    # String for keep leading zeros.
    [int]$PARAM["START_INT"] = $PARAM["START"];
    [int]$PARAM["END_INT"] = $PARAM["END"];
    # Init count leading zero.
    [int]$DIGIT_START = $($PARAM.START).Length;
    [int]$DIGIT_END = $($PARAM.END).Length;
    # Counts for spacing.
    [int]$COUNT_ELEMENTS = $($PARAM.END) - $($PARAM.START) + 1;
    [int]$COUNT_INDEX = $($COUNT_ELEMENTS).Length - 5;
    If ($COUNT_INDEX -le 0) { [int]$COUNT_INDEX = 0; }
    If ($DIGIT_START -gt $DIGIT_END) {
        [int]$COUNT_NAME = $($PARAM.COMPUTER).Length + $DIGIT_START - 4;
    } Else {
        [int]$COUNT_NAME = $($PARAM.COMPUTER).Length + $DIGIT_END - 4;
    }
    [int]$COUNT_STATE = 7;
    [int]$COUNT_LASTLOGON = 9;
    If ($($PARAM.QUIET) -eq $False) { Write-Host "TEST : $($COUNT_ELEMENTS) NAME '$($PARAM.COMPUTER)' - RANGE '$($PARAM.START) to $($PARAM.END)' (min. $DIGIT_START digits / max. $DIGIT_END digits).`n"; }
    # Test : digit logic.
    If ($($PARAM.START) -ge $($PARAM.END)) { Write-Warning "starter digit greater or equal than end digit, EXIT."; Stop-App; }
    If ($($PARAM.QUIET) -eq $False) {
        Write-Host "Index$(" " * ${COUNT_INDEX}) Name$(" " * ${COUNT_NAME}) State LastLogon User";
        Write-Host "-----$("-" * ${COUNT_INDEX}) ----$("-" * ${COUNT_NAME}) ------- --------- ----";
    } 
    # Loop : by range.
    For ($i=$PARAM.START_INT; $i -lt $PARAM.END_INT+1; $i++)  {
        $j = $i - $($PARAM.START_INT);
        $DIGITS = (ConvertTo-Digit -NUMBER $i -DIGIT $DIGIT_START);
        Test-Computer -INDEX $j -NAME "$($PARAM.COMPUTER)$($DIGITS)";
    }
    Write-Host "`nResult exported to '$($PARAM.PATH)\$($PARAM.COMPUTER)-$($CONF.DATE).csv'.";

}