StaleHosts.psm1

$h = "<style>"
$h = $h + "BODY{background-color:white;}"
$h = $h + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$h = $h + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:coral;}"
$h = $h + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:bisque;}"
$h = $h + "</style>"

<#
.Synopsis
Get-StaleComps can be used to return a list of computer objects in Active Directory that have been inactive for a given period of time.
By default, this script will search for computer objects that have been inactive for a year (365 days).
Reports are output as HTML tables to $home\Desktop
Vasken Houdoverdov
 
.Description
 Get-StaleComps can be used to return a list of computer objects in Active Directory that have been inactive for a given period of time.
By default, this script will search for computer objects that have been inactive for a year (365 days).
Reports are output as HTML tables to $home\Desktop
 
.Parameter TimeFrame
This optional parameter represents the number of days the computer object has been inactive for, and must be an integer between 1 and 999.
 
.Example
Get-StaleComps
.Example
Get-StaleComps -TimeFrame 45
#>


function Get-StaleComps
{
    [CmdletBinding()]
    param(
    [ValidateLength(1,3)]
    [String]$TimeFrame = "365",
    [String]$LogFile ='$home\Desktop\Get-StaleComps_errors.txt'
     )
    ${stale comps} = $(Search-ADAccount -accountinactive -computersonly -timespan $TimeFrame  | Sort-Object lastlogondate)
    $x = foreach ($e in ${stale comps})
         {
            $e | Select-Object -Property LastLogonDate,Name,Enabled
         }
    $x | ConvertTo-Html -head $h | Out-File $home\Desktop\Stale_Comps.html
}

<#
.Synopsis
Get-StaleUsers can be used to return a list of user objects in Active Directory that have been inactive for a given period of time.
By default, this script will search for user objects that have been inactive for a year (365 days).
Reports are output as HTML tables to $home\Desktop
Vasken Houdoverdov
 
.Description
Get-StaleUsers can be used to return a list of user objects in Active Directory that have been inactive for a given period of time.
By default, this script will search for user objects that have been inactive for a year (365 days).
Reports are output as HTML tables to $home\Desktop
 
.Parameter TimeFrame
This optional parameter represents the number of days the user account has been inactive for, and must be an integer between 1 and 999.
 
.Example
Get-StaleUsers
.Example
Get-StaleUsers -TimeFrame 45
#>


function Get-StaleUsers
{
    [CmdletBinding()]
    param(
    [ValidateLength(1,3)]
    [String]$TimeFrame = "365",
    [String]$LogFile ='$home\Desktop\Get-StaleUsers_errors.txt'
          ) 
    $d = (Get-Date).Adddays(-($TimeFrame))
    ${stale users} = Get-ADUser -Filter {LastLogonTimeStamp -lt $d -and enabled -eq $true} -Properties LastLogonTimeStamp
    $x = foreach ($e in ${stale users})
         {
          $e | Select-Object -Property Name,GivenName, @{Name="Last Logon";Expression={([DateTime]::FromFiletime([Int64]::Parse($_.LastLogonTimestamp)))}}

         }
      $x | ConvertTo-Html -head $h | Out-File $home\Desktop\Stale_Users.html
}

<#
.Synopsis
Get-EmptyADGroups can be used to return a list of Active Directory security groups that are empty.
Reports are output as HTML tables to $home\Desktop
Vasken Houdoverdov
 
.Description
Get-EmptyADGroups can be used to return a list of Active Directory security groups that are empty.
Reports are output as HTML tables to $home\Desktop
 
.Example
Get-EmptyADGroups
#>


function Get-EmptyADGroups
{
    [CmdletBinding()]
    param(
    [String]$LogFile ='$home\Desktop\Get-EmptyADGroups_errors.txt'
          )
    Get-ADGroup -filter * | where {-Not ($_ | Get-ADGroupMember -ErrorAction SilentlyContinue )}| Select Name | ConvertTo-Html -head $h | Out-File $home\Desktop\Empty_Groups.html
}

Export-ModuleMember -Function * -Cmdlet *