简体   繁体   English

如何从 Powershell 脚本中的 Get-ADUser 过滤器获取结果,以便验证该用户是否存在?

[英]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.

相关问题 在 Powershell 中,如何从 Get-ADUser 结果中删除前 x 个字符? - In Powershell how can I remove the first x number of characters from Get-ADUser results? Get-ADUser(有时)不返回结果)基于属性值的设置方式 - Get-ADUser (sometimes) not returning results) based on how the property value is set 如何在PowerShell脚本中向用户显示要选择的打印机列表? - How can I present the user with a list of printers to choose from in my PowerShell Script? 使用Get-ADUser和Import-Csv进行将来的扩展以使用Set-ADUser更新用户 - Using Get-ADUser with Import-Csv for future expansion to update user using Set-ADUser Powershell:如何获取登录用户的主屏幕分辨率? - Powershell: How can i get the primary screen resolution of a logged in user? 使用Get-ADUser通过PowerShell(显示名称和EmployeeID)进行Active Directory导出 - Active Directory Export via PowerShell (Displayname & EmployeeID) Using Get-ADUser 向 Get-ADUser 提交属性时出现错误? 保存到 CSV 文件 (Powershell) - Bug when submitting properties to Get-ADUser? Saving to CSV File (Powershell) 我如何才能在没有用户输入的情况下运行此脚本 - how can i get this script to run without user input 如何从Powershell中的多维数组中获取值 - How can I get the value from a multidimentional array in Powershell 使用 LDAPFilter anr 匹配 csv 列表中的每个用户的 Get-AdUser 查找 - Get-AdUser Lookup for each user in csv list with LDAPFilter anr match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM