Solved

Disable AD users and add info to a field

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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