0

FIXED: I did have to change if ($null -eq $FoundUser) and then if essentiall NULL -eq NULL, that user doesn't exist... SORRY

I would really love some help as I've on/off been struggling with this logic with Get-ADUser for user creation.

I'm using Powershell 7.0 - 7.2.1 (latter currently) and having this problem with VSCode and running it in "console". We have a 2008 R2 forest but 2016 and 2019 DCs. Essentially "Get-ADUser -Filter" doesn't return any value. I'm trying to use an if statement for if $null -ne $SamAccountName.

Any thoughts? I thought I found the answer here but no luck for me.

$Users = Import-Csv -delimiter "`t" -Path "C:\Users\michel_m\Documents\Scripts\PowerShell\Staff\StaffData.txt"
    #$sam = ""
    #Generate data to use in creating user below
    foreach ($User in $Users) {

    $SAMAccount = $User.Username
    $Filter = "sAmAccountname -eq '$SamAccount'
    $FoundUser = Get-ADUser -Filter $Filter
    Write-Host "HERE IS" $FoundUser
    $results = $FoundUser.SamAccountName

    #if ($null -ne $FoundUser)
    if ($null -ne $results)
    {
        Write-Host $results "Are the results"
        Write-Host $User "is the user"
        Write-Host $SAMAccount "Is the SAM user"
        Write-Host $FoundUser "Is the found user"
     #NewUser_Function
     ($Dates + " - " + $user.username + " has been created") | out-file -filepath $OutputFile1  -append
     
     #Write-Host "Pause 15 seconds"
     Start-Sleep 15
     Write-Host $user.username + " Has been created"
     $Body = $user.username + " Has been created"

     #schtasks.exe /run /s "OHDC01" /tn "GADS Sync - Users"
    }
    else
    {
        Write-Host $Filter
        Write-Host $results "Are the results"
        Write-Host $User "is the user"
        Write-Host $SAMAccount "Is the SAM user"
        Write-Host $FoundUser "Is the found user"
        Write-Host $null is null
     write-host $user.username + " already exists"
     ($Dates + " - " + $user.username + " already exists") | out-file -filepath $OutputFile2  -append
     $Body =  $user.username + " already exists",
                "\\OHDC01\C$\Scripts\StaffCreation\NewStaff\",
                "\\OHDC01\C$\Scripts\StaffCreation\NewStaff\"
    }
}

Output

HERE IS 
sAmAccountname -like 'mangold_m' | Select-object SamAccountName
 Are the results
@{FirstName=Michelle; LastName=Mangold; BuildingName=OAK; Position=Aide; Username=mangold_m; Email=mangold_m@Wonderfullife.org} is the user
mangold_m Is the SAM user
 Is the found user
 is null
mangold_m +  already exists
HERE IS 
sAmAccountname -like 'metzner_m' | Select-object SamAccountName
 Are the results
@{FirstName=Melissa; LastName=Metzner; BuildingName=OHHS; Position=Aide; Username=metzner_m; Email=metzner_m@Wonderfullife.org} is the user
metzner_m Is the SAM user
 Is the found user
 is null
metzner_m +  already exists
Grim
  • 1
  • 1
  • 1
    `| Select-object SamAccountName"` should definitely not be on the `$filter` variable. And you will want to use `-eq` instead of `-like`. – Santiago Squarzon Dec 17 '21 at 14:21
  • Sorry it wasn't supposed to have -like in there. I was grasping at straws at that point. – Grim Dec 17 '21 at 14:39
  • If `Get-ADUser` doesn't return a value, it won't have a field `sAMAccountName`. Test for `$founduser -eq $null`. – Jeff Zeitlin Dec 17 '21 at 14:51
  • Also, check your code. If you did a simple cut-paste, you're missing a closing `"` on the line where you define `$Filter`. – Jeff Zeitlin Dec 17 '21 at 14:54
  • OK, so I fixed it! ...those users don't exist so they are null. Sorry - this has been an on/off type of troubleshooting and the previous logic was definitely NOT working prior to this effort I posted about. – Grim Dec 17 '21 at 15:03
  • @Mat, If my answer is helpful for you, you can Upvote and accept it as answer( click on the check mark beside the answer to toggle it from greyed out to filled in.). This can be beneficial to other community members. Thank you – RahulKumarShaw Jan 03 '22 at 11:16

1 Answers1

0

Thank you for updating you got your solution.

Here I also have tried from my side you can also use this small PowerShell script for validating the user existance.

enter image description here

$Users = Import-Csv -Path "C:\Users\RahulShaw\test.csv"
 Foreach ($User in $Users){

$Username = $User.Username
$FoundUser = Get-ADUser -Filter "sAmAccountName -eq '$Username'"
$results = $FoundUser.SamAccountName
Write-Host $results
if($null -eq $FoundUser){
    write-host "Username '$Username' does not yet exist in active directory" 
}
else{
 write-host "Username '$Username'  exist in active directory" 
}

}

enter image description here

RahulKumarShaw
  • 4,192
  • 2
  • 5
  • 11