Last active
July 28, 2020 06:29
-
-
Save NotNotWrongUsually/4f02315e99d8599c39f13c073da5db86 to your computer and use it in GitHub Desktop.
who function
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 | |
WHO Searches AD for user information. | |
.DESCRIPTION | |
If an exact account name is found it will be shown. If not a search will be performed. Fields searched are | |
sAMAccountName, Name, and Title | |
.PARAMETER Search | |
User to look for in AD. | |
.PARAMETER RetrieveHostname | |
Get the hostname for the resolved user(s). More precisely: gets computer objects in AD whose description | |
matches sAMAccountName | |
.EXAMPLE | |
who <searchterm> | |
Searches AD for the relevant sAMAccountName or actual name if no matching sAMAccountName found. | |
#> | |
Function Who { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory, Position = 0)][string]$Search, | |
[switch]$RetrieveHostname | |
) | |
$entry = [System.DirectoryServices.DirectoryEntry]::new("LDAP://dc=<YOUR_DOMAIN>,dc=com") | |
$filter = "(&(sAMAccountName=$Search)(objectclass=user)(objectCategory=person))" | |
$data = ([adsisearcher]::new($entry, $filter)).FindAll() | |
# No error is raised when performing the search, only when inspecting the result. | |
try { | |
$data | out-null | |
if ($data -ne "") { | |
$got_results = $true | |
} | |
else { | |
$got_results = $false | |
} | |
} | |
catch { | |
# Search filter was broken somehow | |
$got_results = $false | |
} | |
if (-not $got_results) { | |
# Turn something like "firstname lastname" into "*firstname*lastname*" | |
$Search = "*" + ($Search -split " " -join "*") + "*" | |
$filter = "(&(objectclass=user)(objectCategory=person)(&(|(sAMAccountName=*$Search*)(Name=*$Search*)(Title=*$Search*))))" | |
$data = ([adsisearcher]::new($entry, $filter)).FindAll() | |
} | |
$data | ForEach-Object { | |
if ($RetrieveHostname) { | |
$machine_search = ([adsisearcher]"(&(description=$($_.properties["sAMAccountName"]))(objectclass=computer))").FindAll() | |
if ($machine_search -ne "") { | |
$machine_search | ForEach-Object { | |
$hostname += $_.properties["cn"] | |
} | |
} | |
} | |
$output = [PSCustomObject]@{ | |
Name = $_.Properties["Name"] | select -first 1 | |
UserName = $_.Properties["SAMAccountName"] | select -first 1 | |
Title = $_.Properties["Title"] | select -first 1 | |
Mail = $_.Properties["UserPrincipalName"] | select -first 1 | |
Department = $_.Properties["Department"] | select -first 1 | |
Mobile = $_.Properties["Mobile"] | select -first 1 | |
Telephone = $_.Properties["telephonenumber"] | select -first 1 | |
Location = $_.Properties["l"] | select -first 1 | |
Office = $_.Properties["physicaldeliveryofficename"] | select -first 1 | |
} | |
if ($RetrieveHostname) { | |
$output | Add-Member -Name "Hostname" -Value $hostname -MemberType "NoteProperty" | |
$hostname = "" | |
} | |
$output | |
} | |
} | |
<# | |
.SYNOPSIS | |
Shows a form dialogue window with the photo of a user | |
.PARAMETER UserName | |
SAM account name of the user | |
.EXAMPLE | |
Show-UserPhoto <username> | |
.EXAMPLE | |
who <username> | Show-UserPhoto | |
#> | |
function Show-UserPhoto { | |
[CmdletBinding()] | |
param ( | |
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$UserName | |
) | |
$img = Get-UserPhoto $UserName | |
[System.Windows.Forms.Application]::EnableVisualStyles() | |
$form = new-object Windows.Forms.Form | |
$form.Text = "Image Viewer" | |
$form.Width = $img.Size.Width | |
$form.Height = $img.Size.Height | |
$pictureBox = new-object Windows.Forms.PictureBox | |
$pictureBox.Width = $img.Size.Width | |
$pictureBox.Height = $img.Size.Height | |
$pictureBox.Image = $img | |
$form.controls.add($pictureBox) | |
$form.Add_Shown( { $form.Activate() } ) | |
$form.ShowDialog() | |
} | |
<# | |
.SYNOPSIS | |
Returns a bitmap object representing a picture of a user | |
.DESCRIPTION | |
Searches AD for a specific accountname, and returns the associated thumbnailphoto property as a bitmap object. The width and height parameters can be used to scale the image. If only one of width or height is supplied the other will be calculated to keep image proportions. | |
.PARAMETER User | |
A SAMAccountName for a user | |
.PARAMETER Width | |
Desired output width | |
.PARAMETER Height | |
Desire output height | |
.EXAMPLE | |
Get-UserPhoto <SAMAccountName> | |
#> | |
function Get-UserPhoto { | |
[CmdletBinding()] | |
param ( | |
[string]$User, | |
[int]$Width, | |
[int]$Height | |
) | |
$user_data = ([adsisearcher]"(&(objectClass=user)(sAMAccountName=$user))").FindAll() | |
$stream = [System.Io.MemoryStream]::new($user_data.Properties["thumbnailphoto"][0]) | |
$img = [System.Drawing.Image]::FromStream($stream) | |
if ($width -or $height) { | |
if (-not $width) { | |
$width = ($img.Width / $img.height) * $height | |
} | |
if (-not $height) { | |
$height = ($img.height / $img.width) * $width | |
} | |
if ($width -ne $img.Width -or $height -ne $img.Height) { | |
$destination_rect = New-Object System.Drawing.Rectangle 0, 0, $width, $height | |
$destination_image = New-Object System.Drawing.Bitmap $width, $height | |
$destination_image.SetResolution($img.HorizontalResolution, $img.VerticalResolution) | |
$graphics = [System.Drawing.Graphics]::FromImage($destination_image) | |
$graphics.CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceCopy | |
$graphics.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality | |
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality | |
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality | |
$wrapmode = [System.Drawing.Imaging.ImageAttributes]::new() | |
$wrapmode.SetWrapMode([System.Drawing.Drawing2D.WrapMode]::TileFlipXY) | |
$graphics.DrawImage($img, $destination_rect, 0, 0, $img.Width, $img.Height, [System.Drawing.GraphicsUnit]::Pixel, $wrapmode) | |
$destination_image | |
} | |
else { | |
# Width and height parameters don't differ from what the image already has | |
$img | |
} | |
} | |
else { | |
$img | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment