Solved

Hello, i have an issue with a powershell script

Posted on 2014-09-12
8
679 Views
Last Modified: 2014-09-13
HI,
I have an issue with a script that is working fine for the most part. I can import everything from a csv except two fields these are otherTelephone and Country/C/co/CountryCode. these two fields I can import on an individual basis but this is going to be very time consuming.

here are the codes.
1;
This works fine except It does not import those two attributes from above, causes it to error with parameters not found.
# Import AD Module
Import-Module ActiveDirectory

# Import CSV into variable $userscsv

$users = Import-Csv -Path "C:\Users\Administrator\Desktop\ADextracttestserver.csv"
# Loop through CSV and update users if the exist in CSV file
$ExcludeProperties = @(	
    )

foreach ($user in $users)
{
	$Arguments = @{}
    $user | Get-Member -MemberType NoteProperty | ? {$ExcludeProperties -NotContains $_.Name} | % {if ($user.($_.Name)) {$Arguments[$_.Name] = $user.($_.Name)} Else {$Arguments[$_.Name] = $Null}}
	

   #Search in specified OU and Update existing attributes            
	Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "ou=Test Users,DC=testaf,DC=local" | Set-ADUser @Arguments 
}

Open in new window

2;
this works but on an individual basis, the issue here is that I need this to be part of the main script so it runs in bulk querying the users in the csv and I also need it to import the numbers from the csv not a pre defined set like 123 below.
set-aduser testuser -Add @{otherTelephone = 123} 

Open in new window

and finally;
this is the same situation as above, needs to be part of the main script and preferably query the csv if possible but not essential as all users are in the same country.
set-aduser testuser -replace @{C = 'UK'} 

Open in new window

any help will be appreciated.
0
Comment
Question by:BYRONJACKSON
[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
  • 4
  • 3
8 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40320582
Good morning,

If you add otherTelephone and C fields to your CSV file this will let you import them.
# Import AD Module
Import-Module ActiveDirectory

# Import CSV into variable $userscsv

$users = Import-Csv -Path "C:\Users\Administrator\Desktop\ADextracttestserver.csv"
# Loop through CSV and update users if the exist in CSV file
$ExcludeProperties = @("otherTelephone", "C")

foreach ($user in $users) {
  $Arguments = @{}
  $user |
    Get-Member -MemberType NoteProperty |
    ? {$ExcludeProperties -NotContains $_.Name} | 
    % {if ($user.($_.Name)) {$Arguments[$_.Name] = $user.($_.Name)} Else {$Arguments[$_.Name] = $Null}}
	
  #Search in specified OU and Update existing attributes            
	$ADUser = Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "ou=Test Users,DC=testaf,DC=local"
	
	if ($ADUser) {
	  $ADUser | Set-ADUser @Arguments
	  $ADUser | Set-ADUser -Add @{otherTelephone = $user.OtherTelephone}
	  $ADUser | Set-ADUser -Replace @{C = $user.C}
	}
}

Open in new window

HTH

Chris
0
 

Author Comment

by:BYRONJACKSON
ID: 40320760
Hi Chris,

thanks I have them already set in the csv so I will give this a try and get back to you.
0
 

Author Comment

by:BYRONJACKSON
ID: 40320761
Hi,

I now receive this error

Set-ADUser : add
At line:22 char:14
+       $ADUser | Set-ADUser -Add @{otherTelephone = $user.OtherTelephone}
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (CN=Microsoft,OU...Testaf,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 71

Expert Comment

by:Chris Dent
ID: 40320799
Is otherTelephone ever blank in your CSV file?

I suspect it may get upset if you try and feed in an unexpected value (like nothing at all).

Chris
0
 

Author Comment

by:BYRONJACKSON
ID: 40320836
Yes there are a few blank fields as not all users have a number to assign to them, how can I get it to ignore/skip those fields?
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40320871
We can test for the value before we try and set it:
# Import AD Module
Import-Module ActiveDirectory

# Import CSV into variable $userscsv

$users = Import-Csv -Path "C:\Users\Administrator\Desktop\ADextracttestserver.csv"
# Loop through CSV and update users if the exist in CSV file
$ExcludeProperties = @("otherTelephone", "C")

foreach ($user in $users) {
  $Arguments = @{}
  $user |
    Get-Member -MemberType NoteProperty |
    ? {$ExcludeProperties -NotContains $_.Name} | 
    % {if ($user.($_.Name)) {$Arguments[$_.Name] = $user.($_.Name)} Else {$Arguments[$_.Name] = $Null}}
	
  #Search in specified OU and Update existing attributes            
	$ADUser = Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "ou=Test Users,DC=testaf,DC=local"
	
	if ($ADUser) {
	  $ADUser | Set-ADUser @Arguments
    if ($user.OtherTelephone) {
      $ADUser | Set-ADUser -Add @{otherTelephone = $user.OtherTelephone}
    }
    if ($user.C) {
      $ADUser | Set-ADUser -Replace @{C = $user.C}
    }
  }
}

Open in new window

As you can see, I've done the same test for the C value.

Chris
0
 

Author Comment

by:BYRONJACKSON
ID: 40320920
thank you Chris that has worked a charm!!
0

Featured Post

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.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses

617 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