• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • Last Modified:

Vbscript to update AD attributes

Hi Experts,

I need some help with Vbscript to update below attributes in AD

samaccountname,Company,extensionattribute4,extensionattribute1,extensionattribute5,extensionattribute3,office

I have information in CSV file as input

Logging would be of help to check old and new value and if failed or succeded

thanks
KC
0
chandru_sol
Asked:
chandru_sol
  • 20
  • 12
  • 4
  • +1
1 Solution
 
suriyaehnopCommented:
It is possible I propose you to the powershell cmdlet instead
0
 
yo_beeDirector of Information TechnologyCommented:
you can also use CSVDE -i command since you are using a CSV fie.

http://technet.microsoft.com/en-us/library/cc732101(v=ws.10).aspx
0
 
chandru_solAuthor Commented:
Hi suriyaehnop,

I am happy with powershell code. Less number of lines

Logging would help a lot to know if update succeeded or not and old value and new value

Thanks
KC
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell┬« is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
chandru_solAuthor Commented:
I have Quest Active directory role installed too if that will help


Thanks
KC
0
 
yo_beeDirector of Information TechnologyCommented:
Sorry  CSVDE will not meet you needs.
You will need to use PS or VBS.

Sorry for the false reply.
0
 
chandru_solAuthor Commented:
No problem yo_bee. Thanks
0
 
yo_beeDirector of Information TechnologyCommented:
If suriyaehnop does not post or you request that I do, I will post a script suggestion and you can let me know if it works for you?
0
 
chandru_solAuthor Commented:
I will be happy if you could help too. We share knowledge in community :-)

-KC
0
 
yo_beeDirector of Information TechnologyCommented:
Here you go.

import-module activedirectory
#samaccountname,Company,extensionattribute4,extensionattribute1,extensionattribute5,extensionattribute3,office
$csv = import-csv "FileName.csv" -Delimiter ','
ForEach ($line in $csv)
{
$user = Get-ADUser $line.samaccountname -Properties Company,extensionattribute4,extensionattribute1,extensionattribute5,extensionattribute3,physicalDeliveryOfficeName
$user.company = $line.Company 
$user.extensionattribute4 = $line.extensionattribute4
$user.extensionattribute5 = $line.extensionattribute5
$user.extensionattribute3 = $line.extensionattribute3
$user.extensionattribute1 = $line.extensionattribute1
$user.physicalDeliveryOfficeName = $line.office
Set-ADUser -instance $user

Open in new window

0
 
chandru_solAuthor Commented:
yo_bee,

I will try this and let you know. Any help on logging?
0
 
chandru_solAuthor Commented:
Script works only for single domain. Is there a way to search users in all domains and apply changes and log changes


Thanks
KC
0
 
yo_beeDirector of Information TechnologyCommented:
That is a good question.
Are the child domains or differnt domains in a forset that are trusted?
0
 
chandru_solAuthor Commented:
All are child domains in a forest
0
 
chandru_solAuthor Commented:
will there be problem for updating to trusted domains?
0
 
yo_beeDirector of Information TechnologyCommented:
Not sure i have not ever had to.
I would say probably not if your are member of the domain admin.

You may need to handle each instance separately.
0
 
chandru_solAuthor Commented:
So how to handle child domains in forest. Can single script search for a user in forest and update which is not the case now
0
 
yo_beeDirector of Information TechnologyCommented:
Like i said, i have not dealt with this before. You may need to run this on each DC of the child domain.
0
 
yo_beeDirector of Information TechnologyCommented:
Think it might. I will see how I can incorporate it into the script tomorrow.
0
 
yo_beeDirector of Information TechnologyCommented:
I have a question.
Can you get the Distinguished Name (DN) rather than the samAccount?
You might not have to worry about the Child Domain part because the DN points it to there.
0
 
chandru_solAuthor Commented:
I just get samaccountname

If not we need to run that list and find DN for all names and run script. is that possible to get DN for all samaccountname in all domains?
0
 
chandru_solAuthor Commented:
Any update on this script

Thanks
KC
0
 
yo_beeDirector of Information TechnologyCommented:
I do not have a multi-domain forest.

If you run a standard Powershell Script against a user in each Domain what do you get?

import-module activedirectory
#samaccountname,Company,extensionattribute4,extensionattribute1,extensionattribute5,extensionattribute3,office
$csv = import-csv "<FILEPATH>\TestUpdate.csv" -Delimiter ','


ForEach ($line in $csv)
{


Get-ADUser $line.samaccountname | Select DistinguishedName

}

Open in new window

0
 
chandru_solAuthor Commented:
Only get DN for user in current domain where i run script from for all other user who is part of other domain it doesn't work


Thanks
KC
0
 
yo_beeDirector of Information TechnologyCommented:
So I do not think it will work unless you have the DN rather than the samAccount.
Can you run it against each child domain individually?
0
 
chandru_solAuthor Commented:
Yes that is possible. Let me check

I am sure there should be a way. Do you mind if i ask other experts to look into this?

Thanks
KC
0
 
yo_beeDirector of Information TechnologyCommented:
Not at all
0
 
chandru_solAuthor Commented:
Thanks! Any other expert who can help on this script?
0
 
SubsunCommented:
Try this..
$Inputfile = "c:\users.csv"
import-csv -Path $Inputfile | foreach {
$UserGC = Get-QADUser -SamAccountName  $_.User -SearchRoot ""DC=parent,DC=com"" -UseGlobalCatalog
If ($userGC -ne $null)
{Connect-QADService $UserGC.Domain.Dnsname | Out-Null
  $User = Get-QADUser -IncludeAllProperties $UserGC.SamAccountName

#Writing Company
 if ($_.Company -eq $User.Company)
  {Write-host "No need to update Company for "$User.SamAccountName} Else{
   Try{set-qaduser $user.SamAccountName -Company $_.Company -ea Stop | out-null
     Write-Host "Successfully Updated Company of "$User.SamAccountName}
   Catch [system.exception] 
	{Write-Host "Not able to Update Company of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}

#Writing Office
 if ($_.Office -eq $User.Office)
  {Write-host "No need to update Office for "$User.SamAccountName} Else{
   Try{set-qaduser $user.SamAccountName -Office $_.Office -ea Stop | out-null
     Write-Host "Successfully Updated Office of "$User.SamAccountName}
   Catch [system.exception] 
	{Write-Host "Not able to Update Office of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}

#Writing extensionattribute1
 if ($_.extensionattribute1 -eq $User.extensionattribute1)
  {Write-host "No need to update extensionattribute1 for "$User.SamAccountName}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute1"=$_.extensionattribute1} -ea Stop | out-null
     Write-Host "Successfully Updated extensionattribute1 of "$User.SamAccountName}
   Catch [system.exception]
	{Write-Host "Not able to Update extensionattribute1 of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}

#Writing extensionattribute3
 if ($_.extensionattribute3 -eq $User.extensionattribute3)
  {Write-host "No need to update extensionattribute3 for "$User.SamAccountName}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute3"=$_.extensionattribute3} -ea Stop | out-null
     Write-Host "Successfully Updated extensionattribute3 of "$User.SamAccountName}
   Catch [system.exception]
	{Write-Host "Not able to Update extensionattribute3 of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}
			
#Writing extensionattribute4
 if ($_.extensionattribute4 -eq $User.extensionattribute4)
  {Write-host "No need to update extensionattribute4 for "$User.SamAccountName}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute4"=$_.extensionattribute4} -ea Stop | out-null
     Write-Host "Successfully Updated extensionattribute4 of "$User.SamAccountName}
   Catch [system.exception]
	{Write-Host "Not able to Update extensionattribute4 of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}

#Writing extensionattribute5
 if ($_.extensionattribute5 -eq $User.extensionattribute5)
  {Write-host "No need to update extensionattribute5 for "$User.SamAccountName}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute5"=$_.extensionattribute5} -ea Stop | out-null
     Write-Host "Successfully Updated extensionattribute5 of "$User.SamAccountName}
   Catch [system.exception]
	{Write-Host "Not able to Update extensionattribute5 of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}

#Writing SamAccountName
 if ($_.SamAccountName -eq $User.SamAccountName)
  {Write-host "No need to update SamAccountName for "$User.SamAccountName} Else{
   Try{set-qaduser $user.SamAccountName -SamAccountName $_.SamAccountName -ea Stop | out-null
     Write-Host "Successfully Updated SamAccountName of "$User.SamAccountName}
   Catch [system.exception]
	{Write-Host "Not able to Update SamAccountName of $($User.SamAccountName) Error : $($_.Exception.Message)" -B Yellow -F Red}}
}Else {Write-Host "Not able to find User "$_.SamAccountName -B Yellow -F Red}
}

Open in new window

CSV Format
User,SamAccountName,Company,extensionattribute4,extensionattribute1,extensionattribute5,extensionattribute3,office
User2,User13,EE,EA4,EA1,EA5,EA3,Ohio
User3,User14,EE,EA4,EA1,EA5,EA3,Ohio

Open in new window

0
 
chandru_solAuthor Commented:
Just 2 additions

1. How to update manager attribute? Manager attribute is also samaccountname
2. How to make sure logs are cleared and emailed

Thanks
0
 
chandru_solAuthor Commented:
Logs are created and emailed. Thanks
0
 
SubsunCommented:
Try..
$Inputfile = "c:\users.csv"
$Lofile = "C:\Temp\log.txt"
import-csv -Path $Inputfile | foreach {

$UserGC = Get-QADUser -SamAccountName  $_.User -SearchRoot ""DC=parent,DC=com"" -UseGlobalCatalog
$Manager = get-qaduser $_.manager

If ($userGC -ne $null)
{Connect-QADService $UserGC.Domain.Dnsname | Out-Null
  $User = Get-QADUser -IncludeAllProperties $UserGC.SamAccountName

#Writing Company
 if ($_.Company -eq $User.Company)
  { Add-Content $Logfile "No need to update Company for $($User.SamAccountName)"} Else{
   Try{set-qaduser $user.SamAccountName -Company $_.Company -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated Company of $($User.SamAccountName)"}
   Catch [system.exception] 
	{ Add-Content $Logfile "Not able to Update Company of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing Office
 if ($_.Office -eq $User.Office)
  { Add-Content $Logfile "No need to update Office for $($User.SamAccountName)"} Else{
   Try{set-qaduser $user.SamAccountName -Office $_.Office -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated Office of $($User.SamAccountName)"}
   Catch [system.exception] 
	{ Add-Content $Logfile "Not able to Update Office of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing extensionattribute1
 if ($_.extensionattribute1 -eq $User.extensionattribute1)
  { Add-Content $Logfile "No need to update extensionattribute1 for $($User.SamAccountName)"}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute1"=$_.extensionattribute1} -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated extensionattribute1 of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update extensionattribute1 of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing extensionattribute3
 if ($_.extensionattribute3 -eq $User.extensionattribute3)
  { Add-Content $Logfile "No need to update extensionattribute3 for $($User.SamAccountName)"}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute3"=$_.extensionattribute3} -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated extensionattribute3 of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update extensionattribute3 of $($User.SamAccountName) Error : $($_.Exception.Message)"}}
			
#Writing extensionattribute4
 if ($_.extensionattribute4 -eq $User.extensionattribute4)
  { Add-Content $Logfile "No need to update extensionattribute4 for $($User.SamAccountName)"}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute4"=$_.extensionattribute4} -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated extensionattribute4 of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update extensionattribute4 of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing extensionattribute5
 if ($_.extensionattribute5 -eq $User.extensionattribute5)
  { Add-Content $Logfile "No need to update extensionattribute5 for $($User.SamAccountName)"}Else{
   Try{set-qaduser $user.SamAccountName -objectattributes @{"extensionattribute5"=$_.extensionattribute5} -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated extensionattribute5 of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update extensionattribute5 of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing SamAccountName
 if ($_.SamAccountName -eq $User.SamAccountName)
  { Add-Content $Logfile "No need to update SamAccountName for $($User.SamAccountName)"} Else{
   Try{set-qaduser $user.SamAccountName -SamAccountName $_.SamAccountName -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated SamAccountName of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update SamAccountName of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

#Writing Manager
if ($UserGC.Manager -eq $Manager.dn)
  { Add-Content $Logfile "No need to update Manager for $($User.SamAccountName)"} Else{
   Try{set-qaduser $user.SamAccountName -Manager $Manager -ea Stop | out-null
      Add-Content $Logfile "Successfully Updated Manager of $($User.SamAccountName)"}
   Catch [system.exception]
	{ Add-Content $Logfile "Not able to Update Manager of $($User.SamAccountName) Error : $($_.Exception.Message)"}}

}Else { Add-Content $Logfile "Not able to find User "$_.SamAccountName}
}
Send-MailMessage -Subject "Script Report" -From from@domain.com -To to@domain.com -Attachments $Logfile -SmtpServer smtp.domain.com

Open in new window

0
 
chandru_solAuthor Commented:
Thanks Subsun!! it worked and i made few changes i would like to share to optimise time taken

$Manager = get-qaduser $_.manager
to
$Manager = Get-QADuser -SamAccountName $_.manager -SearchRoot "DC=iconcr,DC=com" -UseGlobalCatalog

$User = Get-QADUser -IncludeAllProperties $UserGC.SamAccountName
to
  $User = Get-QADUser -DontUseDefaultIncludedProperties -IncludedProperties extensionattribute1,extensionattribute3,extensionattribute4,extensionattribute5,manager $UserGC.SamAccountName

Thanks
0
 
chandru_solAuthor Commented:
Thanks Subsun! I will start reading powershell and start helping users on this community :-)
0
 
SubsunCommented:
It should be good..
0
 
chandru_solAuthor Commented:
Forgot to mention after changes for single user it takes 0.5 seconds and without changes it takes 1.2 minutes :-)
0
 
SubsunCommented:
Great!!..it makes lot of difference when you do bulk modification..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 20
  • 12
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now