【问题标题】:Using a .txt file to populate a Powershell Script使用 .txt 文件填充 Powershell 脚本
【发布时间】:2018-07-13 01:00:49
【问题描述】:

我有一个 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"   

【问题讨论】:

  • 您需要出示您的脚本以获得帮助
  • 使用Get-Content,然后从结果数据构造一个循环。网上有很多关于如何做到这一点的文章。试一试,然后回来。
  • 我尝试了一个答案,但没有您现有脚本的内容以及您的 users.txt 会是什么样子,因此无法确切知道您的要求。

标签: powershell active-directory


【解决方案1】:

假设您的文本文件是一个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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    相关资源
    最近更新 更多