Solved

Disable AD users and add info to a field

Posted on 2016-10-01
5
32 Views
Last Modified: 2016-10-02
Can someone show me how to put these two commands together ?  they work separately but want to know how to combine these two so then I can follow the same format for updating other fields if necessary .

#This disables the accounts in the text file.
Get-Content users.txt | Disable-ADAccount

#this adds a note to the -office field
get-content users.txt | Set-ADUser -Office "MyNotes $(Get-date)"
0
Comment
Question by:MilesLogan
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:footech
ID: 41825028
Generally you've got two options when you want to process an array of objects (which is what a typical Get-Content command produces) and take multiple actions on the contents of that array.  Either:
 1) try to form a single pipeline, where all the objects get piped directly from one command to another; or,
 2) use a looping construct to call the commands separately for each object of the array.

Whether you can do number 1 depends on two things:  the output of one command, and what type of input another command accepts.  If these two things line up, you're good to go.  Here's an example.
Get-Content users.txt | Set-ADUser -Office "MyNotes $(Get-date)" -Passthru | Disable-ADAccount

Open in new window


Number 2 is more flexible as there's no dependency on input and output types.  You can use a number of looping constructs including ForEach-Object, foreach statement, for, etc.  Inside the loop you could pipe the current object to each of the commands, or provide it directly with the input parameter (I've included one of each below).  Example:
Get-Content users.txt | ForEach-Object {
    Set-ADUser -Identity $_ -Office "MyNotes $(Get-date)"
    $_ | Disable-ADAccount
}

Open in new window

0
 
LVL 2

Author Comment

by:MilesLogan
ID: 41825049
Hi Footech , thank you for the explanation and details .. works as expected .. can you help me understand ..Why would modifying line 3 to what is below not move the object ?

$_ | Disable-ADAccount | Move-ADObject -TargetPath "MyPATH"
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 41825095
Because by default the Disable-ADAcoount cmdlet doesn't produce any output, so it's not sending anything to Move-ADObject.  You need to add the -Passthru parameter (should work, but I haven't tested).
0
 
LVL 32

Expert Comment

by:ste5an
ID: 41825167
Just a comment: Create also a separaete OU named Disabled Users and move the accounts. This simplifies a lot of processes and reduces traffic.
0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 41825424
Hi footech

-Passthru worked , it makes a lot of sense how you explained it and I see it in the help syntax .. thank you for taking the time to explain it , I appreciate it .

SYNTAX
    Disable-ADAccount [-Identity] <ADAccount> [-AuthType {Negotiate | Basic}] [-Credential <PSCredential>] [-Partition <string>] [-PassThru <switch>]
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now