我有一個來自 Get-ADUser 的結果串列,它為我提供了一個 OU 中的所有用戶。輸出用戶名的格式是“-prefix-username”。我需要洗掉 7 個字符“-prefix-”,然后對剩余的“用戶名”部分進行另一個 Get-ADUser 查找。我發現的問題是,如果我只運行第二個 Get-ADUser 查找,我將 $User 設定為一個特定的“-prefix-username”,它作業正常,但是當我嘗試處理串列時,我要么得到一個錯誤修剪后的用戶名后似乎有空格(txt 格式串列 - Get-ADUser : 找不到身份為“user ”下的物件:)或用戶名包含一個“我無法從用戶名末尾洗掉”( csv 格式串列 - Get-ADUser:找不到身份為“用戶”的物件)。
到目前為止,我有:
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
uj5u.com熱心網友回復:
首先要提到的是,如果您使用 來創建串列Select -ExpandProperty SAMAccountName,您將只會在檔案中獲得 SamAccountnames。
話雖如此,為什么還要費心使用“中間”檔案,而只需執行以下操作:
# 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'
uj5u.com熱心網友回復:
您可能會遇到將其保存到檔案(格式化的地方)然后重新讀取的問題。格式化可能是添加"和讀取換行符(您認為是空格)字符。如果您確實需要保存它,請執行以下操作(否則只需連接管道):
$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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366179.html
上一篇:為什么[IO.File]::WriteAllText連接Get-Content-tail##輸出,而Set-Content不連接?
下一篇:在PowershellI中回圈
