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