BYRONJACKSON
asked on
Hello, i have an issue with a powershell script
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.
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.
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.
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
}
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}
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'}
any help will be appreciated.
ASKER
Hi Chris,
thanks I have them already set in the csv so I will give this a try and get back to you.
thanks I have them already set in the csv so I will give this a try and get back to you.
ASKER
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:A DUser) [Set-ADUser], ADInvalidOperationExceptio n
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Mi crosoft.Ac tiveDirect ory.Manage ment.Comma nds.SetADU ser
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,
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Mi
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
I suspect it may get upset if you try and feed in an unexpected value (like nothing at all).
Chris
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you Chris that has worked a charm!!
If you add otherTelephone and C fields to your CSV file this will let you import them.
Open in new window
HTHChris