简体   繁体   English

在 Powershell 中,如何从 Get-ADUser 结果中删除前 x 个字符?

[英]In Powershell how can I remove the first x number of characters from Get-ADUser results?

I have a list of results from Get-ADUser giving me all users in an OU.我有一个来自 Get-ADUser 的结果列表,它为我提供了 OU 中的所有用户。 The format of the output username is '-prefix-username'. output 用户名的格式为“-prefix-username”。 I need to remove the 7 character '-prefix-' and then conduct another Get-ADUser lookup against the remaining 'username' portions.我需要删除 7 个字符“-prefix-”,然后对剩余的“用户名”部分进行另一次 Get-ADUser 查找。 The issue I'm finding is that if I run just the second Get-ADUser lookup where I set $User as just one specific '-prefix-username' it works fine but when I try to process a list I either get an error where there seems to be space after the trimmed username (txt format list - Get-ADUser: Cannot find an object with identity: 'user ' under:) or the username includes a " that I can't remove from the end of the username (csv format list - Get-ADUser: Cannot find an object with identity: 'user"').我发现的问题是,如果我只运行第二个 Get-ADUser 查找,我将 $User 设置为一个特定的“-prefix-username”,它工作正常,但是当我尝试处理一个列表时,我要么在哪里得到一个错误修剪后的用户名后似乎有空格(txt 格式列表 - Get-ADUser:找不到具有身份的 object:)或用户名包含“我无法从用户名末尾删除”( csv 格式列表 - 获取 ADUser:找不到身份为“用户”的 object)。

So far I have:到目前为止,我有:

get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 | 
Select SAMAccountName | 
Out-File C:\Temp\UserList.txt

$UserList = (Get-Content C:\Temp\UserList.txt)

$StandardUsers = ForEach($User in $UserList) {
   
    Write-Host "Now checking $User"
    Get-ADUser $User.Substring(7) -Properties * | 
    Select-object DisplayName, UserPrincipalName, Mail, Manager,EmployeeID    
    }
    $StandardUsers | Out-File -FilePath C:\Temp\StandardUserList.txt

First thing to mention is that if you create the list using Select -ExpandProperty SAMAccountName , you would only get SamAccountnames in the file.首先要提到的是,如果您使用Select -ExpandProperty SAMAccountName创建列表,您只会在文件中获得 SamAccountnames。

Having said that, why bother with an 'in-between' file at all and simply do:话虽如此,为什么还要为“中间”文件而烦恼,而只需这样做:

# By default, Get-ADUser returns these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# Only ask for properties that are not already in this list.
Get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 -Properties DisplayName, EmailAddress, Manager, EmployeeId |
    Select-Object DisplayName, UserPrincipalName, EmailAddress, Manager,EmployeeID |  
    Set-Content -Path 'C:\Temp\StandardUserList.txt'

You are likely having issues with saving it to a file (where it gets formatted) and then reading it back in. The formatting could be adding " and reading a newline (which you think is a space) character. If you really need to save it then do the following (else just hook up the pipelines):您可能在将其保存到文件(格式化)然后将其读回时遇到问题。格式可能是添加"并读取换行符(您认为是空格)字符。如果您真的需要保存然后它执行以下操作(否则只需连接管道):

$userList = Get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 | 
  Select-Object SAMAccountName
$userList | 
  Out-File C:\Temp\UserList.txt

$standardUsers = $userList |
  Select-Object -ExpandProperty SAMAccountName -PipelineVariable user |
  ForEach-Object {
    Write-Host "Now checking $user"
    $userWithoutPrefix = ($user -Replace '^-prefix-','') -Replace '(\w|\n)$','' # to use a more advanced version of the suggestion by @Avshalom
    Get-ADUser $userWithoutPrefix -Properties * | Write-Output
  } | 
  Select-Object DisplayName, UserPrincipalName, Mail, Manager, EmployeeID

$standardUsers | Out-File -FilePath C:\Temp\StandardUserList.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 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? Get-ADUser(有时)不返回结果)基于属性值的设置方式 - Get-ADUser (sometimes) not returning results) based on how the property value is set 使用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) 使用Get-ADUser和Import-Csv进行将来的扩展以使用Set-ADUser更新用户 - Using Get-ADUser with Import-Csv for future expansion to update user using Set-ADUser 如何获取 Powershell 中的 CPU 核心数? - How can I get the number of CPU cores in Powershell? 使用 LDAPFilter anr 匹配 csv 列表中的每个用户的 Get-AdUser 查找 - Get-AdUser Lookup for each user in csv list with LDAPFilter anr match 为什么Active Directory主目录查询在Get-ADuser中返回的内容与在AD管理面板中返回的内容不同? - Why Active Directory Home Directory query returns different in Get-ADuser than in AD admin panel? Get-ADUser - output 未找到数据时 CSV 文件的空白行 - Get-ADUser - output a blank line fo CSV file when no data is found 如何在批处理文件或PowerShell脚本中运行命令“ x”次? - How can I run a command 'x' number of times, in batch file or PowerShell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM