Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Disable AD users and add info to a field

Posted on 2016-10-01
5
Medium Priority
?
56 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 41

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 41

Accepted Solution

by:
footech earned 2000 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 35

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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