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