?
Solved

How to read/query Active Directory in real time without being read by a variable

Posted on 2013-01-08
4
Medium Priority
?
323 Views
Last Modified: 2013-01-09
If i query AD and add all the information on variable $ad, the contents are in memory and not a realtime stage.

So, if i do modify properties of an AD user, all those modification are done through $ad via Set-ADUser.

In every property change i prompt "are you sure about the change of this for that?" and give the opportunity of changing more properties of the same user by asking for it.

But, if i typed a wrong last name and the time comes to ask "are you sure about the change of this for that" $ad has the old value not the new value. Is there anyway to query AD in real time?

<code>
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
</code>

Thanks for your help,
0
Comment
Question by:namerg
[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
4 Comments
 
LVL 40

Assisted Solution

by:footech
footech earned 1000 total points
ID: 38757200
This question is a little hard to understand outside the context of your question at
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27987781.html
so here's a link back to it.

So in your script you've made a query for all users in AD and stored them to a variable.  As you work through each of them with a foreach loop, anytime you need to make another query it's pretty much the same command that you have above, and filter/match it with the current object being handled by the foreach loop.
Get-ADUser -filter {samAccountName -like "$AD_User.samAccountName"} -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com"

Open in new window

May have to use $($AD_User.samAccountName) in place of $AD_User.samAccountName in the above.
0
 

Author Comment

by:namerg
ID: 38757212
Yes, @footech I know is little hard to understand. Working on the flow chart.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 1000 total points
ID: 38758147
You can either do
$File = Import-Csv "c:\scripts\ad\temp\FILE.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber

Foreach ($User in $FILE){
$Ad = Get-ADUser -Filter {Surname -eq $User.Surname} -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
 	$Ad | % {
"Your Script to modify"
 }
}

Open in new window

Or
$File = Import-Csv "c:\scripts\ad\temp\FILE.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber

Foreach ($User in $FILE){
	$Ad | ? {$_.sn -eq $User.sn} | % {
"Your Script to modify"
 }
}

Open in new window

0
 

Author Comment

by:namerg
ID: 38759658
@Subsun,
Can you check the flowchart diagram in the bottom on this question http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27987781.html

Let me know what you think.. thanks
0

Featured Post

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Suggested Courses

762 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