77 lines
2.5 KiB
PowerShell
77 lines
2.5 KiB
PowerShell
function Get-SHSUser {
|
|
|
|
param (
|
|
[parameter(ValueFromPipeline)]
|
|
[string]$user,
|
|
[switch]$groups
|
|
)
|
|
if($null -eq (get-module -ListAvailable -Name 'ActiveDirectory')){
|
|
Write-Warning 'Active Drirectory Thick Client is required for this function'
|
|
return
|
|
}
|
|
Write-Progress -Activity "Getting user data for $name" -Status 'Querying AD for user data' -PercentComplete 30 -ParentId 1
|
|
if($user -match "^[\d\.]+$"){
|
|
$UserInfo = Get-ADUser -Filter { EmployeeID -eq $user } -properties *
|
|
$user = $UserInfo.SamAccountName
|
|
}
|
|
if($user -match "\w \w"){
|
|
$UserInfo = Get-ADUser -Filter { DisplayName -eq $user } -Properties *
|
|
$user = $UserInfo.SamAccountName
|
|
}
|
|
else {
|
|
$UserInfo = Get-ADUser -Identity $user -Properties *
|
|
}
|
|
if($null -eq $UserInfo){
|
|
if ($groups) { return @() }
|
|
$props = [ordered]@{
|
|
Name="User does not exist in AD"
|
|
UserName=$null
|
|
EmployeeID=$null
|
|
Title=$null
|
|
Department=$null
|
|
Manager=$null
|
|
Phone=$null
|
|
Email=$null
|
|
Computers=$null
|
|
LastLogon=$null
|
|
}
|
|
$obj = New-Object -TypeName PSObject -Property $props
|
|
|
|
return $obj
|
|
}
|
|
|
|
if ($groups) {
|
|
$res = @()
|
|
foreach ($group in $UserInfo.MemberOf) {
|
|
if (-not ($group -match "CN=([^,]*),")) { continue }
|
|
$res += [PSCustomObject]@{
|
|
Name = $Matches[1]
|
|
Username = $user
|
|
}
|
|
}
|
|
return $res
|
|
}
|
|
|
|
$manager = $UserInfo.Manager.Split("=,")[1]
|
|
|
|
Write-Progress -Activity "Getting user data for $name" -Status 'Querying SCCM for LastLogon data' -PercentComplete 80 -ParentId 1
|
|
|
|
$computerSCCM = Get-SCCM_UserLastLoggedOn $user
|
|
$computerList = $computerSCCM -join ", "
|
|
$props = [ordered]@{
|
|
Name=$UserInfo.DisplayName
|
|
UserName=$user
|
|
EmployeeID=$UserInfo.EmployeeID
|
|
Title=$UserInfo.Title
|
|
Department=$UserInfo.Department
|
|
Manager=$manager
|
|
Phone=$UserInfo.telephoneNumber
|
|
Email=$UserInfo.EmailAddress
|
|
Computers=$computerSCCM # User $computerList for more readable, $computerSCCM for more scriptable
|
|
LastLogon=$UserInfo.LastLogonDate
|
|
}
|
|
|
|
$obj = New-Object -TypeName PSObject -Property $props
|
|
|
|
return $obj
|
|
} |