Nov 01 2020 11:16 AM
Hi,
I need to return a list of active users: Display name etc. but no data is exported to my out-file. Any clues would be highly appreciated . Thanks
$users = Get-Content "C:\dump\usernames2.txt"
foreach ($user in $users)
{
if (Get-ADUser -ldapfilter "(samaccountname=$user)" -Properties displayname,mail,distinguishedName | Select-Object -Property displayname,mail,distinguishedName)
{
out-file "c:\dump\usersexist.txt"
}
else
{
Add-Content "C:\dump\namenotfound.txt" "$user"
}
}
I
Nov 01 2020 11:25 AM
Nov 01 2020 11:42 AM - edited Nov 01 2020 11:43 AM
yes, the file contains SamAccountNames
I tried the -identity para but received the error below
Get-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:26
+ if (Get-ADUser -identity ($_.$User) -Properties displayname,mail,dist ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Get-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:26
+ if (Get-ADUser -identity ($_.$User) -Properties displayname,mail,dist ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Nov 01 2020 03:59 PM
Nov 01 2020 10:05 PM
Hi
What are you expecting from the out-file, where is the inputObject, at least you will need to pipeline it with another command.
if you are talking about dumping the console output to a file, then this is a console redirection, read about it here.
anyway
$users = Get-Content "C:\usernames2.txt"
foreach ($user in $users)
{
try{
$Value=Get-ADUser -ldapfilter "(samaccountname=$user)" -Properties displayname,mail,distinguishedName -ErrorAction stop| Select-Object -Property displayname,mail,distinguishedName
if ($Value.count -eq 0){
Add-Content "C:\namenotfound.txt" "$($user)"
}
else{
add-Content -Path 'C:\UserFound.txt' -Value $Value
}
}
catch{
Write-Host $_.exception.message
}
}
Check the code above