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"] = "CC BY-SA 4.0"; # =============================== # # CONFIGURATION # # # DESCRIPTION : everything that configures the APP. $CONF = @{}; $CONF["DATE"] = Get-Date; $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"]) { Launch-Terminate 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]; } Else { If ($PARAM["RANGE"]) { Launch-Terminate 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([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 " - $NAME exists and online (user '$USERLOGGED')" -ForegroundColor DarkRed; } } Else { $FILE = "$NAME;online;$LASTLOGON;private"; If ($($PARAM.QUIET) -eq $False) { Write-Host " - $NAME exists and online" -ForegroundColor DarkRed; } } } Else { $FILE = "$NAME;offline;$LASTLOGON;"; If ($($PARAM.QUIET) -eq $False) { Write-Host " - $NAME exists and offline (last logon : '$LASTLOGON')" -ForegroundColor Yellow; } } } Else { $FILE = "$NAME;free;never;"; If ($($PARAM.QUIET) -eq $False) { Write-Host " - $NAME free" -ForegroundColor DarkGreen; } } $FILE | Add-Content -Path "$($PARAM.PATH)\$($APP.SHORTNAME).csv"; } # ====================== # # CALL # # # DESCRIPTION : starting calls by the APP. # CSV file init. Import-Module ActiveDirectory; $FILE = ""; Test-File -PATH "$($PARAM.PATH)\$($APP.SHORTNAME).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. $DIGIT = $($PARAM.START).Length; If ($($PARAM.QUIET) -eq $False) { Write-Host "`n FUNCTION EXECUTED FROM '$($CONF.PATH)' AT '$($CONF.DATE)'."; Write-Host "`n FUNCTION PARAMETERS : PATH '$($PARAM.PATH)' - COMPUTER '$($PARAM.COMPUTER)' - RANGE '$($PARAM.START) to $($PARAM.END)' (min. $DIGIT digits).`n"; } # Loop : by range. For ($i=$PARAM.START_INT; $i -lt $PARAM.END_INT+1; $i++) { $j = $i - $($PARAM.START_INT); $DIGITS = Set-Digit $i $DIGIT; If ($($PARAM.QUIET) -eq $False) { Write-Host " test #$j : '$($PARAM.COMPUTER)$($DIGITS)'"; } Test-Computer "$($PARAM.COMPUTER)$($DIGITS)"; } } |