I have a problem importing phone numbers from a CSV file based on email addresses to Active directory using a PowerShell script.
The table contains:
mail;telephoneNumber
toto#domaine.com;88888888
tata#domaine.com;99999999
here’s the code I’m running but it shows me an error message, or I don’t see why there’s this message:
Import-module ActiveDirectory
Import-CSV E: scripts list.csv |
ForEach-Object {
Write-Host "telephoneNumber $($_.telephoneNumber)"
Get-ADUser -Filter "mail -like '$($_.mail)'" |
Set-ADUser -telephoneNumber $_. telephoneNumber}
Here is the error message:
telephoneNumber
Set-ADUser: Unable to find a parameter corresponding to the name «telephoneNumber».
Character E: scripts employeeid.ps1:6: 14
+ Set-ADUser -telephoneNumber $_. telephoneNumber}
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId: NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
NB: I am a beginner in the subject
Thank you well in advance for your help
I tried this code too but still the same problem.
Import-module ActiveDirectory
Import-CSV "E:\scripts\liste.csv" | % {
$telephoneNumber = $_.telephoneNumber
$mail= $ail_.m
Set-ADUser $telephoneNumber -mail $mail
}
The LDAP property telephoneNumber is known as OfficePhone in PowerShell and LDAP property mail has a PowerShell equivalent called EmailAddress.
Cmdlet Set-ADUser does not have a parameter called telephoneNumber, but it does have OfficePhone, so a rewrite of your code would be
Import-Module ActiveDirectory
Import-Csv -Path 'E:\scripts\list.csv' | ForEach-Object {
$user = Get-ADUser -Filter "mail -eq '$($_.mail)'" # or use PS equivalent 'EmailAddress'
if ($user) {
Write-Host "Setting telephoneNumber $($_.telephoneNumber) for $($user.Name)"
$user | Set-ADUser -OfficePhone $_.telephoneNumber
# if you do want to use LDAP property telephoneNumber, you can use below
# $user | Set-ADUser -replace #{telephoneNumber = $($_.telephoneNumber)}
}
else {
Write-Warning "Could not find user with EmailAddress $($_.mail)"
}
}
P.S. you made some typos when posting:
E: scripts list.csv is missing the backslashes
$_. telephoneNumber has a space between the dot and the property name
Related
I have a .csv file that I am using to modify custom attributes on users in Active Directory, but PowerShell does not like the script:
Import-Csv -path c:\users\user\desktop\doc.csv | ForEach-Object {
Set-ADUser $_.mail -replace #{
ExtensionAttribute1 = $_.ExtensionAttribute1
}
}
I get the following error:
Set-ADUser : replace
At line:2 char:4
Set-ADUser $_.mail -replace #{
CategoryInfo: InvalidOperation: (user123:ADUser) [Set-ADUser], ADInvalidOperationException
FullyQualifiedErrorId: ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
The CSV only has 2 columns:
extensionAttribute1,mail
Any help would be appreciated
The -Identity parameter for Set-ADUser does not take an email address.
It needs either the DistinguishedName, objectGUID, SID or SamAccountName. You can also pipe a user object directly to the cmdlet.
Because of that, you need to first try to find the user with Get-ADUser and if that succeeds set the attribute.
Import-Csv -Path 'c:\users\user\desktop\doc.csv' | ForEach-Object {
$user = Get-ADUser -Filter "EmailAddress -eq '$($_.mail)'" -ErrorAction SilentlyContinue
if ($user) {
$user | Set-ADUser -Replace #{ extensionAttribute1 = $_.extensionAttribute1 }
}
else {
Write-Warning "No user with email address '$($_.mail)' found.."
}
}
PS. I always use the exact LDAP name inside the Hash for the key name when using -Add, -Replace etc. Case sensitive.
I'm not so good at PowerShell. I have this script which worked before that I've have used to update the manager attribute in Active Directory, however, now after just replacing the file its no longer working
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
$user= Get-ADUser -Filter "displayname -eq '$($Users.EmployeeFullName)'"|select -ExpandProperty samaccountname
$manager=Get-ADUser -Filter "displayname -eq '$($Users.'Line Manager Fullname')'"|select -ExpandProperty DistinguishedName
Set-ADUser -Identity $user -Replace #{manager=$manager}
}
when running the script it returns:
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:6 char:22
+ Set-ADUser -Identity $user -Replace #{manager=$manager}
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
the "EmployeeFullName" and "Line Manager FullName" format in the .csv is set as i.e. joe bloggs, see below CSV file example.
EmployeeFullname Line Manager Fullname
---------------- ---------------------
Kieran Rhodes Tracey Barley
Lewis Pontin Tracey Barley
Lizzy Wilks Rodney Bennett
I also noticed if I remove and try to retype "$Users.EmployeeFullName" it no longer picks up the "EmployeeFullName" from the csv file.
Anyone know what I'm doing wrong here please?
Below would be an example:
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
$user = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'"
$manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName
Set-ADUser -Identity $User -Replace #{manager=$manager}
}
Note: you don't need to dig down to the samAccountName property because Set-ADUser will take the full fidelity object for the -Identity argument. You also don't need to use Select... -ExpandProperty you can just parenthetically dot reference the distinguishedName property.
Also you can use the instancing capability in the AD cmdlets:
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ( $User in $Users )
{
$User = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'" -Properties Manager
$Manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName
$User.Manager = $Manager
Set-ADUser -Instance $User
}
In this case you call back the Manager property set it with a simple assignment statement than give the $User variable as the argument to the -Instance parameter of Set-ADUser
I was busy making a script where a variable is declared containing the information of the user that you can enter in a prompt.
I thought of this way when I saw that unlock account can't be done with an e-mail address (Logon name after 2000).
But I can't figure out what I am doing wrong.
This is the code i am running right now.
Import-Module ActiveDirectory
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "$env:username")
$Test = Get-ADUser -Filter { EmailAddress -eq $name } | Select SamAccountName
Unlock-ADAccount -Identity $Test
The error i'm getting is
Unlock-ADAccount : Object reference not set to an instance of an object.
At C:\Users\Admcbl\Documents\Powershell.ps1:5 char:1
+ Unlock-ADAccount -Identity $Test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Activ...ement.ADAccount:ADAccount) [Unlock-ADAccount], NullReferenceException
+ FullyQualifiedErrorId : Object reference not set to an instance of an object.,Microsoft.ActiveDirectory.Management.Commands.UnlockADAccount
You should refer to the SamAccountName property of $Test:
Unlock-ADAccount -Identity $Test.SamAccountName
But why not just pipe the account to the Unlock-ADAccount? Like this:
Get-ADUser -Filter { EmailAddress -eq $name } | Unlock-ADAccount
Here might be a simpler solution for you. Notice I switched the filter so #domain.com isn't needed
Import-Module ActiveDirectory
$name = Read-Host -Prompt "Enter Username "
Get-ADUser -Filter {SamAccountName -eq $name} |
Unlock-ADAccount
I'm using a PowerShell script to add some information to all users in AD. For some reason, I keep getting an error message if the user has a apostrophe in their name (e.g Tim O'Reilly).
How can I format the script so it will include names with apostrophe ?
My script:
# Import AD Module
Import-Module ActiveDirectory
write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $users
$users = Import-Csv -Path C:\Scripts\users.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -SearchBase "DC=My,DC=domain,DC=com" |
Set-ADUser -Company $($user.Email)
}
Write-Host 'done!' -ForegroundColor Green
And this is the error message I'm getting:
Get-ADUser : Error parsing query: 'displayName -eq 'Tim O'Reilly''
Error Message: 'syntax error' at position: '29'. At
C:\Scripts\Update-information\Update-Users-2.ps1:13 char:1
+ Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
osoft.ActiveDirectory.Management.Commands.GetADUser
I'd really appreciate any help I can get here.
Thank you,
You can use some custom delimiter in your csv file instead of modifying your script. Just divide your data with custom char like ":" (Tim : O'Reilly), and add delimiter switch for import-csv cmdlet, like
$users = Import-Csv -Path C:\Scripts\users.csv -Delimiter :
I have to change all UPN in my Active Directory. Some UPN are completely empty an some are filled.
Now I want to fill them all with their sAMAccountName.
My script looks like this:
# Import AD Module
Import-Module ActiveDirectory
# Import CSV into variable $userscsv
#$userscsv = import-csv C:\temp\rename\usernames.csv
$users = Import-Csv -Path C:\temp\rename\usernames.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "sAMAccountName -eq '$($user.samaccountname)'"
Set-ADUser -userPrincipalName $($users.samaccountname)
}
I get an errorcode:
Get-ADUser : Der Suchfilter wurde nicht erkannt
Bei C:\TEMP\RENAME\User-Rename.ps1:13 Zeichen:12
+ Get-ADUser <<<< -Filter "sAMAccountName -eq '$($user.samaccountname)'"
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : Der Suchfilter wurde nicht erkannt,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Does anyone have an idea?
I would not recommend setting the UPN to the SamAccountName. That's not how it's supposed to be.
If you must do it anyway, you can't use subexpressions in a filter. Assign the value to a variable and use that in the filter expression. You also need to pipe the user object into Set-ADUser to give the cmdlet something to work with:
foreach ($user in $users) {
$acct = $user.SamAccountName
Get-ADUser -Filter "sAMAccountName -eq '$acct'" |
Set-ADUser -userPrincipalName $acct
}