Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Disable AD users and add info to a field

Posted on 2016-10-01
5
42 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 40

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 40

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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