Last active
April 4, 2016 21:40
-
-
Save bundyfx/b1dd85acbc1f9db32e544ee71dd9066b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Return basic details on AD objects | |
.EXAMPLE | |
Get-ADObjectInfo -Computers | |
.EXAMPLE | |
Get-ADObjectInfo -Users | Format-Table -Autosize | |
.EXAMPLE | |
Get-ADObjectInfo -Users | Export-Csv -Path C:\Users.csv -nti | |
#> | |
function Get-ADObjectInfo | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[String] | |
[ValidateNotNullOrEmpty()] | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[ValidateSet("Users", "Computers", "GPO", "Groups")] | |
$Target | |
) | |
Begin | |
{ | |
Try { | |
Import-Module ActiveDirectory -ErrorAction Stop | |
Import-Module GroupPolicy -ErrorAction Stop | |
} catch [Exception] | |
{ | |
throw 'You will need the ActiveDirectory/GroupPolicy module for this function to execute' | |
} | |
} | |
Process | |
{ | |
if ($PSBoundParameters.ContainsValue('Users')){ | |
$allusers = Get-ADUser -filter * -Properties LastLogonTimestamp,userPrincipalName,Name,Logoncount,DistinguishedName,Enabled | |
foreach($user in $allusers) | |
{ | |
[Array]$ObjUsers += "" | Select-Object @{Name='Name';Expression={$User.Name}}, | |
@{Name='UPN';Expression={$User.userPrincipalName}}, | |
@{Name='LogonCount';Expression={$User.Logoncount}}, | |
@{Name='DistinguishedName';Expression={$User.DistinguishedName}}, | |
@{Name='Enabled';Expression={$User.Enabled}}, | |
@{Name='LastLogonTimestamp';Expression={[DateTime]::FromFileTime($user.LastLogonTimeStamp)}} | |
} | |
} | |
if ($PSBoundParameters.ContainsValue('Computers')){ | |
$allComputers = Get-ADComputer -filter * -Properties SID,OperatingSystemVersion,Enabled,Name,IPv4Address,PrimaryGroup,Enabled | |
foreach($computer in $allComputers) | |
{ | |
[Array]$ObjComp += "" | Select-Object @{Name='Name';Expression={$computer.Name}}, | |
@{Name='SID';Expression={$computer.SID}}, | |
@{Name='OperatingSystemVersion';Expression={$computer.OperatingSystemVersion}}, | |
@{Name='Enabled';Expression={$computer.Enabled}}, | |
@{Name='IP';Expression={$computer.IPv4Address}}, | |
@{Name='PrimaryGroup';Expression={$computer.PrimaryGroup}} | |
} | |
} | |
if ($PSBoundParameters.ContainsValue('GPO')){ | |
$allGPO = Get-GPO -All | Select-Object GpoStatus,CreationTime,ModificationTime,DisplayName | |
foreach($policy in $allGPO) | |
{ | |
[Array]$ObjGPO += "" | Select-Object @{Name='GPOStatus';Expression={$policy.GpoStatus}}, | |
@{Name='CreationTime';Expression={$policy.CreationTime}}, | |
@{Name='ModificationTime';Expression={$policy.ModificationTime}}, | |
@{Name='DisplayName';Expression={$policy.DisplayName}} | |
} | |
} | |
if ($PSBoundParameters.ContainsValue('Groups')){ | |
$allGroups = Get-ADGroup -Filter * -Properties whenCreated,whenChanged,Name,CanonicalName,GroupCategory,GroupScope | |
foreach($Group in $allGroups) | |
{ | |
[Array]$ObjGroup += "" | Select-Object @{Name='WhenCreated';Expression={$Group.whenCreated}}, | |
@{Name='WhenChanged';Expression={$Group.whenChanged}}, | |
@{Name='Name';Expression={$Group.Name}}, | |
@{Name='CanonicalName';Expression={$Group.CanonicalName}}, | |
@{Name='Category';Expression={$Group.GroupCategory}}, | |
@{Name='Scope';Expression={$Group.GroupScope}} | |
} | |
} | |
} | |
End | |
{ | |
switch ($PSBoundParameters.Values) { | |
Users {Write-Output $ObjUsers} | |
Computers {Write-Output $ObjComp} | |
GPO {Write-Output $ObjGPO} | |
Groups {Write-Output $ObjGroup} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment