使用.txt文件填充Powershell脚本

时间:2018-07-10 23:40:22

标签: powershell active-directory

我有一个Powershell脚本,该脚本按用户ID查询Active Directory,并输出到列出我指定的用户属性的.csv文件中。

该脚本可以完美地为单个用户使用,但是我想使用带有用户列表的文本文件来填充脚本并一次为多个用户创建输出。

请告知。

这是我原始的脚本,用于从特定的OU中提取用户列表:
 导入模块ActiveDirectory

$ADGroupParams=@{ 
'Server' = 'corp.xxxx.com' 
'Searchbase' = 'OU=Security Groups,DC=corp,DC=xxxx,DC=com' 
'Searchscope'= 'Subtree' 
'Filter' = '*' 
'Properties' = '*' 
} 
 $SelectParams=@{ 
'Property' = 'cn', 'managedBy', 'info', 'sAMAccountName', 'created' 
} 
 get-aduser @ADGroupParams | select-object @SelectParams  | export-csv "C:\Scripts\Users_By_OU.csv"   

1 个答案:

答案 0 :(得分:0)

假设您的文本文件是txt文件,每行一个用户名:

# Put all ADUser properties you want dumped to the CSV in an array
$adUserProperties = @(
  'one',
  'attribute',
  'per',
  'index'
)

# Read the users from the file, get the ADUser object for each one,
# selecting the properties you want dumped to the CSV, and export the
# returned information to a CSV file.
Get-Content \path\to\users.txt | Foreach-Object {
  Get-ADUser -Filter "SamAccountName -eq '$($_)'" -Properties $adUserProperties
} | Select-Object -Properties $adUserProperties | Export-Csv -NoTypeInformation users.csv