Thursday, July 7, 2016

Powershell Profile - Time Savers

Powershell Profile - Time Savers

These are some of the functions I use in my Powershell Profile. Currently, most of the profile I use is specific to my environment. In order to post here, I'm steadily redacting the sensitive information. Additionally, I'm going through and making a configuration file so that environmental settings can all be edited in one place. 
For now though, here are a few bits and pieces that I think may be of use to you.

There is a lot of work to be done in standardizing the functions so that they follow my own personal conventions, as well as accept input as a standard parameter. 

#Returns the Windows Version

function Get-WindowsVersion(){ 
   return (Get-WmiObject -ComputerName $ComputerName -Class Win32_OperatingSystem).Caption 

#Get-User Queries AD for User and saves object to $Global:ttkUser. I use this variable name in my own personal convention. Other functions reference ttkUser when a username or object is expected, but has not been provided. 

function Get-User{
Param([Parameter(Mandatory=$true)][string]$User, [switch]$Extended = $false)
$search = "*" + $User + "*" 
$Global:ttkUser = Get-ADUser -filter {DisplayName -like $search -or UserPrincipalName -like $search} -Properties *
$Global:ttkUser | Select SamAccountName,DisplayName, Office, EmployeeNumber, badgeNumber, Title, MobilePhone, OfficePhone, DistinguishedName, Created | FL
Write-Host -ForegroundColor Cyan "Saved ADUser to `$Global:ttkUser..."
Set-Alias User Get-User

#Get-TTKUser selects the first object from $Global:TTKUser (if present) and sanitizes the data. Get-User is designed to return all matching results, with the first result being the "most likely" candidate. 

function Get-ttkUser{
Write-Host -ForegroundColor Cyan "Selecting First user in ttkUser"
if (!$Global:ttkUser.samaccountname) {
Write-Host -ForegroundColor Red "ttkUser does not contain any users. Run Get-User first"
else {
if ($Global:ttkUser.samaccountname[0].gettype().name -eq "Char"){
$UserName = $Global:ttkUser.samaccountname
Write-Host -ForegroundColor Yellow "`$UserName is set to $username"
$UserName = $Global:ttkUser.samaccountname[0]
Write-Host -ForegroundColor Yellow "`$UserName is set to $username"
return $UserName
#Get-UserGroups returns a list of the Active Directory Security groups the user is a part of. 
function Get-UserGroups{
$user = Get-ttkUser
$user| Get-ADPrincipalGroupMembership | select name
Set-Alias Groups Get-UserGroups

#Get-LockoutTime returns a timestamp of when the user account was locked out. AD stores this attribute as an epoch time.  
function Get-LockoutTime{
[long]$epoch = (get-ttkUser).lockoutTime
return [datetime]::FromFileTime([long]$epoch)
#Unlock-User -  I think this is pretty self explanatory.
function Unlock-User{
Unlock-ADAccount -identity (get-ttkUser)
Set-Alias Unlock Unlock-User 

No comments:

Post a Comment