Home > Software engineering >  How to reference a property from another command?
How to reference a property from another command?

Time:11-07

So im trying to setup a script for unlocking users accounts.

Right now this is what i have:

function unlocktm

{
$user = read-host "Who is the user?"

Unlock-ADAccount -Identity $user -Credential adminaccount

Write-host "Account for $user has been unlocked!"
}

now here is the difficult part, our users usernames are based off their email. and we switched email formats like last year. So some peoples emails are [email protected] and others are [email protected]

So when that script i have now asks "who is the user?" i have to put in and know that users username style (either first.lastname or flastname). Well with 1500 users, i dont exactly know every ones emails off the top of my head.

So what im trying to do is make it so that when it asks for "who is this user?" i can just put in John Doe, itll run a second script i have setup that can search for that user in AD, pull the attribute and apply it into the first script. Let me explain better. For the time being i have a second script that is this:

function findtm

{
$Firstname = Read-Host "What is the users Firstname?"
$Lastname = Read-Host "What is the Users Lastname?"
Get-ADUser -Filter "Surname -like '$Lastname*' -and GivenName -like '$Firstname*'"
}

and that pulls the info about that user and shows me what their email style is. That email style is under the "samaccountname".

So ultimately, what i need it to do is:

I run the function, it asks me for the user.

I can just put in "John Doe"

and it will then run that second script in the background that searches for "John Doe" and then if it finds a John Doe, its grabs the "samaccountname" property (either john.doe or jdoe), attaches that to the answer in replace of the "john doe" and then the script runs the unlockad command for john doe and unlocks his account.

Im probably over thinking this by a large margin, but i was thinking a If-else could do it but i have no clue how to to set it up to pull that "samaccountname" property.

CodePudding user response:

In my opinion you should think of your functions as independent units, that take in the information they need as parameters. I would typically leave prompting or user interaction to the outermost layer.

Consider that Get-ADUser returns an object that can be used with Unlock-ADAccount as well.

function unlocktm
{
param($Identity)

Unlock-ADAccount -Identity $Identity -Credential adminaccount

Write-host "Account for $($Identity.SamAccountName) has been unlocked!"
}

function findtm
{
param($Firstname, $Lastname)
Get-ADUser -Filter "Surname -like '$Lastname*' -and GivenName -like '$Firstname*'"
}

# the outer script area
$Firstname = Read-Host "What is the users Firstname?"
$Lastname = Read-Host "What is the Users Lastname?"

$Id = findtm -Firstname $Firstname -Lastname $Lastname
unlocktm -Identity $id

Now, if you want to make it a little more fancy:

function unlocktm
{
  [CmdletBinding()]
  param(
    [Parameter(ValueFromPipeline=$true)]
    $Identity
  )

  Process {
    Unlock-ADAccount -Identity $Identity -Credential adminaccount

    Write-host "Account for $($Identity.SamAccountName) has been unlocked!"
  }
}

function findtm
{
param($Firstname, $Lastname)
Get-ADUser -Filter "Surname -like '$Lastname*' -and GivenName -like '$Firstname*'"
# by simply calling this, the value returned becomes the return value of your function
}

# the outer script area
$Firstname = Read-Host "What is the users Firstname?"
$Lastname = Read-Host "What is the Users Lastname?"

findtm -Firstname $Firstname -Lastname $Lastname | unlocktm

There are lots of possibilities, but perhaps this is a good start?

  • Related