[英]How can I get results from Get-ADUser Filter in my Powershell script so I can validate if that user exists or not correctly?
FIXED: I did have to change if ($null -eq $FoundUser) and then if essentiall NULL -eq NULL , that user doesn't exist... SORRY修复:我确实必须更改 if ($null -eq $FoundUser) ,然后如果根本NULL -eq NULL ,该用户不存在...对不起
I would really love some help as I've on/off been struggling with this logic with Get-ADUser for user creation.我真的很想得到一些帮助,因为我一直在努力使用 Get-ADUser 来创建用户的这种逻辑。
I'm using Powershell 7.0 - 7.2.1 (latter currently) and having this problem with VSCode and running it in "console".我正在使用 Powershell 7.0 - 7.2.1(目前是后者)并且在 VSCode 中遇到这个问题并在“控制台”中运行它。 We have a 2008 R2 forest but 2016 and 2019 DCs.我们有一个 2008 R2 森林,但有 2016 和 2019 DC。 Essentially "Get-ADUser -Filter" doesn't return any value.本质上,“Get-ADUser -Filter”不返回任何值。 I'm trying to use an if statement for if $null -ne $SamAccountName.我正在尝试对 if $null -ne $SamAccountName 使用 if 语句。
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 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
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.在这里,我也尝试过,您也可以使用这个小的 PowerShell 脚本来验证用户的存在。
$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"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.