Cannot Search AD from CSV using Powershell - 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 }

Related

Get AD Groups Notes and Description Field From Importing CSV File

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

Import-CSV to Get-ADUser

I am trying to make a very simple script work that reads a CSV with 100 SamAccountName/Identities into a variable and then returns AD Users with all Objects from Get-ADUser.
The CSV only contains usernames (1 each line) so essentially it could also be a simple text file.
I feel like I'm very close but I can't make it work for some reason.
$users= (Import-CSV 'C:\Temp\users.csv') # <-- Doesn't work
#$users= "backupservice","name01" <-- This Works
ForEach ($user in $users)
{
Get-ADUser $user -Properties *
}
Here's the error message when I am trying to read the CSV into the loop:
Get-ADUser : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At line:8 char:16
+ Get-ADUser $user -Properties *
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Okay, so I have an issue with parameter binding but I am not sure how to fix it.
I would appreciate any input/insight you could give me.
Thank you!
I actually just now took the original CSV (which had two columns [SamAccountName and UPN]) and fixed it just now with the following code:
$users = (Import-CSV "C:\Temp\Test001.csv" | Select SamAccountName -ExpandProperty SamAccountName)
ForEach ($user in $users)
{
Get-ADUser $user -Properties * #| Export-CSV 'C:\Temp\users_expanded.csv'-Append
}
Is there a better way to do this? The journey continues.

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
}

How to use the variable to replace the Object/OU Path with LDAPFilter in Powershell

I am trying to retrieve the membership for a specific office of one specific security group in our working environment, instead of Get-ADGroupMember which is slow and always get time-out when there is a huge user list.
My code is as below:
Import-module ActiveDirectory
**$groupinfo** = Get-ADGroup -identity "vip"
Get-ADuser -LDAPFilter '(&(objectcategory=user)(memberof='**$groupinfo.DistinguishedName**'))' -Properties office,title | where {$_.office -like 'New York'} | select name,samaccountname,office,title |Export-csv -NoTypeInformation c:\tmp\NY.csv -Delimiter ";"
I get the following error
Get-ADUser : A positional parameter cannot be found that accepts argument '**CN=vip,OU=Groups,DC=contoso,DC=com**'.
At line:2 char:2
+ Get-ADuser -LDAPFilter '(&(objectcategory=user)(memberof='$group.Dis ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Can anyone advise me how to use this variable $groupinfo in LDAPFilter?
Do I need a junction?
Get-ADuser -LDAPFilter '(&(objectcategory=user)(memberof=**CN=vip,OU=Groups,DC=contoso,DC=com**))' -Properties office,title | where {$_.office -like 'New York'} | select name,samaccountname,office,title |Export-csv -NoTypeInformation c:\tmp\NY.csv -Delimiter ";"
This one does work when no variable.
If you use double-quotes around the LDAPFilter, the content of the variable is used instead of the variable name literal.
Try:
Get-ADuser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(memberOf=$($groupinfo.DistinguishedName)))" -Properties office,title |
Where-Object {$_.office -like '*New York*'} |
Select-Object name,samaccountname,office,title |
Export-Csv -NoTypeInformation c:\tmp\NY.csv -Delimiter ";"
Note: I have not tried to put all this in a single ling, because doing that just askes for mistakes that are hard to spot. Also, I changed (objectcategory=user) to (objectCategory=person)(objectClass=user) to make sure only user objects are returned. See Filter on objectCategory and objectClasO

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}
}