StaleHosts.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
$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 *