Get AD Groups Notes and Description Field From Importing CSV File - powershell

I'm trying to import a .csv file that contains multiple AD groups and I want to get only the notes and Description field. I'm currently getting errors, and I'm not sure what I'm doing incorrectly. Any help will be appreciated.
$ADGroups = import-csv "C:\Users\User\Documents\ADNotesField.csv"
foreach ($ADGroup in $ADGroups)
{ Get-ADGroup -Identity $ADGroup -Properties info, description
}
$ADGroup | Export-CSV -Path "C:\Users\User\Documents.csv" -NoTypeInformation
This is the error that I'm receiving:
Get-ADGroup : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported.
At line:3 char:25
+ { Get-ADGroup -identity $ADGroups -Properties info, description
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADGroup], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADGroup
CSV File first few lines starting at cell A1 and going down to A2, etc. All of these are AD Groups:
Domain Users
Washington Techs
California Techs
Nevada Techs

If you really have a CSV (comma-delimited) and considering the Users column has the AD Groups you need to query, the following should work:
Import-Csv "C:\Users\User\Documents\ADNotesField.csv" | ForEach-Object {
try {
Get-ADGroup -Identity $_.Users -Properties info, description
}
catch {
Write-Warning $_.Exception.Message
}
} | Select-Object Name, Info, Description |
Export-CSV -Path "C:\Users\User\Documents.csv" -NoTypeInformation

Related

Importing csv data into custom attribute in Active Directory

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.

Active directory delete description and office fields through powershell

I have a list of users in active directory to which I have to remove the description and office fields. how can i do through poweshell? I try this but not work.
My code path is ok
Import-Module ActiveDirectory
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv
foreach($User in $Users){
Set-ADUser $User.SamAccountName -Description $User.NewDescription
}
my csv
SamAccountName;NewDescription;EmailAddress
xxxxxxxxxx;xxxxxxxxxxx;xxxxxxxxxxxxx#libero.it
Powershell answer
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:4 char:12
+ Set-ADUser $User.SamAccountName -Description $User.NewDescription
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUs
er
Help will be appreciated
You need to specify your delimiter on your import-csv statement. The default is comma separated, so you can define it like so
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv -Delimiter ';'
Without this, then it's importing as one single column instead of three.
See Import-CSV for more.
It might work?
$users = Import-Csv -Path C:\users1.csv
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “cn=Users,DC=**********,DC=*****” |
Set-ADUser -Clear description, Office
}

Cannot Search AD from CSV using Powershell

Trying to search AD account properties pulling from a CSV. The Import-CSV line works by itself. I cannot for the life of me figure out why it is asking for a filter. I took this from another script I found where they said it worked. Others were using a For-Each statement.
PS C:\Users\XXXXX> Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties displayname
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument
collection contains a null value.
At line:1 char:43
+ Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Its prompting you for the 'Identity' because that is how it identifies what user you are searching for.
Try this:
$users = get-content C:\Temp\test.csv
foreach ($user in $users){Get-ADUser -Identity $user -Properties displayname}
The CSV file just has the user IDs for the users who you would like to find info for.
Or if you can try the following:
Import-Csv C:\Temp\users.csv | ForEach-Object { Get-ADUser -identity $_.Name -Properties displayname }

Get-ADGroup with filter isn't working for all

I got a problem with my powershell script, I need to get all users from group, I have the group id which I can use to get the group. The problem I have is that my solution isn't working for all group and I don't get what is wrong.
I have some group name
eAM
eGR
eTE
eDF
eMP-arts
e-CV
The 3 first isn't working and the other one yes. Here the script I use
$like = "*" + $branch
foreach ($member in (Get-ADGroupMember (Get-ADGroup -filter {name -like $like}))){
# Do something
}
And the error I get for which isn't working
Get-ADGroupMember : can't convert «System.Object[]» in «Microsoft.ActiveDirectory.Management.ADGroup»,
requiered by «Identity» param. The specified method is not supported
+ Get-ADGroupMember (Get-ADGroup -filter {name -like "*AM"})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument : (:) [Get-ADGroupMember], ParameterBindingException
+ FullyQualifiedErrorId : cannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
Thanks in advance for your help,
MYT
The error thells you that Get-ADGroupMember does not accept an array of groups, but a single group. You'd also get errors if Get-AdGroup would not return any results. Instead, pipe the commands:
Get-ADGroup -Filter {name -like $like} | Get-ADGroupMember | Foreach-Object {
}
Please note that accounts can belong to multiple groups so same member could be returned multiple times.

How to populate the manager field on Active Directory, using powerShell

Im trying to populate the manager fields on AD using
Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $_.samAccountName -Replace #{Manager=$_.manager}}
But I get the following error:
Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception
setting " Replace": "Object reference not set to an instance of an
object." At line:1 char:95
+ Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $.samAcc ountName -Replace <<<< #{manager=$.manager}}
+ CategoryInfo : WriteError: (:) [Set-ADUser], ParameterBindingEx ception
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
.Management.Commands.SetADUser
Depending on what the Manager is represented by in your csv you can just use the Set-Aduser parameter -Manager on its own.
Import-Csv C:\Testimport.csv | ForEach-Object {Set-ADUser -Identity $_.samAccountName -Manager $_.manager}
If not please show some sample date in your question. This would work in Manager was a account name at least. Also there is an error in the code you ran: manager=$.manager should be manager=$_.manager
If $_.manager is the Manager sAMAccountName attribute, you need the distinguidhedName attribute to use it later as a value into -Replace parameter:
Import-Csv C:\Testimport.csv |
ForEach-Object {
$managerDN = (Get-ADUser $_.manager).distinguishedName
Set-ADUser -Identity $_.samAccountName -Replace #{Manager=$managerDN}
}