PowerShell导出CSV时排除特定GivenName与SN的方法求助
解决PowerShell导出AD用户CSV时排除特定用户的问题
你只需要在现有的Where-Object筛选环节,加入排除GivenName包含"Agile"、SN包含"External"用户的逻辑即可。另外注意你脚本里的Dd=corp,Dd=ads是笔误,应该改成DC=corp,DC=ads,否则会导致AD查询失败。
修改后的完整脚本:
Get-ADUser -SearchBase "OU=Us00,OU=NA,DC=corp,DC=ads" -Filter {Enabled -eq $True} -Properties GivenName, SN, DisplayName, Company, LastLogonDate | Select-Object GivenName, SN, DisplayName, Company, LastLogonDate | Where-Object { $_.LastLogonDate -lt (Get-Date).AddDays(-30) -and $_.LastLogonDate -ne $null -and -not ($_.GivenName -like "*Agile*") -and -not ($_.SN -like "*External*") } | Export-Csv -Path G:\Conduct\InactiveUsers.csv -NoTypeInformation
关键修改说明:
- 修正了LDAP路径里的
Dd为DC(Domain Component的正确缩写) - 在
Where-Object中新增了两个排除条件:-not ($_.GivenName -like "*Agile*"):排除GivenName字段包含"Agile"的用户,*是通配符,匹配任意长度的字符-not ($_.SN -like "*External*"):排除SN(姓)字段包含"External"的用户
- 把
-Properties *改成了明确需要的属性,提升查询效率,避免加载不必要的AD用户属性
内容的提问来源于stack exchange,提问作者KeepVBSalive
相关产品推荐
相关产品推荐

