简体   繁体   English

如何使用 PowerShell 从 Active Directory 合并多行信息属性

[英]How to merge multiline info property from Active Directory using PowerShell

I need to create CSV file (one line per AD account) with account's Samaccountname and Info from MS AD and then load data from CSV file into SQL table.我需要使用来自 MS AD 的帐户的 Samaccountname 和信息创建 CSV 文件(每个 AD 帐户一行),然后将 CSV 文件中的数据加载到 SQL 表中。 Many accounts have multiline Info property in AD, like the following: PS SQLSERVER:> $InfoCheckInfo许多帐户在 AD 中具有多行 Info 属性,如下所示:PS SQLSERVER:> $InfoCheckInfo
####00123 ####00123

Task9009- no valid feed - 9/10/14
Disabled due to feed 9-12-2014

Feed updated, 9-15-2014

PS SQLSERVER:\> 

Multiline Info property can include empty lines.多行信息属性可以包含空行。

My solution: Step1.我的解决方案:步骤 1。 $Result= $(Get-ADUser $adUserCheck -Properties * | select SamAccountName) $Result= aaa123 $Result= $(Get-ADUser $adUserCheck -Properties * | select SamAccountName) $Result= aaa123

Step2.第2步。 I replaced empty lines and merge data: PS SQLSERVER:> $InfoCheckInfo = $(Get-ADUser $adUserCheck -Properties info | Select-Object - ExpandProperty info) -replace "`n", ";"我替换了空行并合并数据: PS SQLSERVER:> $InfoCheckInfo = $(Get-ADUser $adUserCheck -Properties info | Select-Object - ExpandProperty info) -replace "`n", ";"

PS SQLSERVER:\> $InfoCheckInfo
####00123;;;Task9009- no valid feed - 9/10/14;Disabled due to feed 9-12-2014;;Feed updated, 9-15-2014

Step3.第三步。 $Result | $结果 | Add-Member -NotePropertyName info $InfoCheckInfo Step4. Add-Member -NotePropertyName info $InfoCheckInfo Step4. $Result | $结果 | export-csv -Delimiter '~' "D:\test\ad_user_info.csv" -notype export-csv -Delimiter '~' "D:\test\ad_user_info.csv" -notype

The problem is that if Info had multiline property, CSV file creates four lines of data, like this:问题是如果 Info 具有多行属性,CSV 文件会创建四行数据,如下所示:

aaa123 ####00123;;;
Task9009- no valid feed - 9/10/14;
Disabled due to feed 9-12-2014;;
Feed updated, 9-15-2014

I need one line for each account- aaa123 ####00123;;;Task9009- no valid feed - 9/10/14;Disabled due to feed 9-12-2014;;Feed updated, 9-15-2014我需要为每个帐户设置一行 - aaa123 ####00123;;;Task9009- 没有有效的提要 - 9/10/14;由于提要 9-12-2014 已禁用;;提要已更新,2014 年 9 月 15 日

Please help, Thanks请帮忙,谢谢

I personally would filter out all user objects 1st that will need to be exported and then built PowerShell object in 1 line.我个人会首先过滤掉所有需要导出的用户对象,然后在 1 行中构建 PowerShell object。 You can rewrite same with in long form with Foreach loop您可以使用 Foreach 循环以长格式重写相同的内容

Another note, I have done a quick test with multiple lines in StreetAddress and the newline character used was另请注意,我在 StreetAddress 中对多行进行了快速测试,使用的换行符是

`r`n

so you should use it in your replace.所以你应该在你的替换中使用它。

This worked for me (use appropriate filter for your use case)这对我有用(为您的用例使用适当的过滤器)

Get-ADUser -Filter * -Properties info | 
select SamAccountName, @{N="CheckInfo"; E={$_.Info.replace("`r`n", ";")}} | 
export-csv D:\test\ad_user_info.csv -NoTypeInformation -Delimiter '~'

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

相关问题 在Powershell中读取CSV文件以从Active Directory获取信息 - Reading CSV file in powershell to get info from Active directory 获取活动目录信息时如何在powershell中处理带连字符的名称 - How to handle hyphenated names in powershell when getting active directory info 如何使用Powershell从活动目录中获取独特的部门? - How to get unique departments from active directory using Powershell? 如何使用Powershell在Active Directory中添加用户 - How to Add user in Active directory using Powershell 如何从powershell显示活动目录浏览器 - How to display active directory browser from powershell 是否可以使用Powershell在Active Directory中设置用户memberOf属性 - Is it possible to set a users memberOf property in Active Directory using Powershell Powershell从Active Directory中将MobilePhone属性分配给变量 - Powershell Assign MobilePhone property from Active Directory to variable PowerShell 中的正则表达式从 Active Directory 中的 Managedby 属性获取城市名称 - Regex in PowerShell to get the city name from the Managedby property in Active Directory Powershell Active Directory - 更新用户的“姓名首字母”属性 - Powershell Active Directory - Update a users `Initials` property Exchange PowerShell:查找活动目录办公室属性 - exchange powershell : finding the active directory office property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM