Solved

Disable AD users and add info to a field

Posted on 2016-10-01
5
34 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 33

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

10 Experts available now in Live!

Get 1:1 Help Now