英文:
PSCustomObject list CSV only one column
问题
称我为PowerShell初学者可能有些高估了我。我试图阅读不同的Microsoft文章和StackOverflow线程,它们非常有帮助,但我遇到了一个让我感到困惑的问题。我正在尝试遍历我们的AD组织单位,检查每个组织单位中的每个用户,输出任何密码将在14天内过期的用户。我有一个成功的脚本可以做到这一点,但现在我想添加额外的列到CSV中,以便我可以获得其他数据,比如电子邮件地址、密码到期日期等等。经过多次研究,这是我想出的代码,但它将两个对象属性(Email和PWExpires)输出到同一CSV列中。
任何帮助都将不胜感激!
$CutOffDate = (get-date).AddDays(-166).Date
$pwExpDate = $User.PasswordLastSet
$MaxCutOffDate = (get-date).AddDays(-365).Date
$pwExpList = [System.Collections.Generic.List[System.Object]]::new()
$OUs=
#deleted for posting
foreach($OU in $OUs) {
$Users = Get-ADUser -filter * -SearchBase $OU -properties *
foreach($User in $Users) {
if(($User.PasswordLastSet -lt $CutOffDate) -and ($User.PasswordLastSet-gt $MaxCutOffDate) -and ($User.EmailAddress -notlike 'noreply*') -and ($User.EmailAddress -notlike '')) {
$userObj = [PSCustomObject]@{
Email = $User.EmailAddress
PwExpires = $User.PasswordLastSet.AddDays(180)
}
$pwExpList.Add($userObj)
#$pwExpList.Add($User.PasswordLastSet.AddDays(180))
}
}
}
$pwExpList | Out-File -FilePath G:\pwExpList2.csv
英文:
Calling me a Powershell beginner would be giving me too much credit. I'm trying to read different Microsoft articles and StackOverflow threads, and they've been extremely helpful, but I've hit a point that's got me stumped. I'm trying to run through our AD OUs, checking every user in each OU, spitting out any user whose password is going to expire within 14~ days. I have a successful script that does just that, but now I'm trying to add extra columns into the CSV it spits out so I can have additional data to go off of like email address, password expiry date, etc. After much research this is what I've come up with, but it's spitting the two object properties (Email and PWExpires) into the same CSV column.
Any assistance is greatly appreciated!
$CutOffDate = (get-date).AddDays(-166).Date
$pwExpDate = $User.PasswordLastSet
$MaxCutOffDate = (get-date).AddDays(-365).Date
$pwExpList = [System.Collections.Generic.List[System.Object]]::new()
$OUs=
#deleted for posting
foreach($OU in $OUs) {
$Users = Get-ADUser -filter * -SearchBase $OU -properties *
foreach($User in $Users) {
if(($User.PasswordLastSet -lt $CutOffDate) -and ($User.PasswordLastSet-gt $MaxCutOffDate) -and ($User.EmailAddress -notlike 'noreply*') -and ($User.EmailAddress -notlike '')) {
$userObj = [PSCustomObject]@{
Email = $User.EmailAddress
PwExpires = $User.PasswordLastSet.AddDays(180)
}
$pwExpList.Add($userObj)
#$pwExpList.Add($User.PasswordLastSet.AddDays(180))
}
}
}
$pwExpList | Out-File -FilePath G:\pwExpList2.csv
答案1
得分: 1
Use Export-Csv
和保持简单,而不是尝试创建列表,将使处理数据变得更容易:
$CutOffDate = (get-date).AddDays(-166).Date
$pwExpDate = $User.PasswordLastSet
$MaxCutOffDate = (get-date).AddDays(-365).Date
# 无需预先创建输出的列表对象 - 请参见下面的代码
$OUs =
# 此处已删除用于发布的内容
$pwExpList = $OUs | ForEach-Object {
# 遍历所有组织单位
Get-ADUser -filter * -SearchBase $_ -Properties * | ForEach-Object {
# 遍历所有找到的用户
If (($_.PasswordLastSet -lt $CutOffDate) -and ($_.PasswordLastSet -gt $MaxCutOffDate) -and ($_.EmailAddress -notlike 'noreply*') -and ($_.EmailAddress -notlike '')) {
[PSCustomObject]@{
Email = $_.EmailAddress
PwExpires = $_.PasswordLastSet.AddDays(180)
}
}
}
}
# 输出到 csv 文件
$pwExpList | Export-Csv -Path C:\temp\output.csv -NoTypeInformation
英文:
Use Export-Csv
and if you keep things simple rather than trying to make lists, you will make handling data much easier:
$CutOffDate = (get-date).AddDays(-166).Date
$pwExpDate = $User.PasswordLastSet
$MaxCutOffDate = (get-date).AddDays(-365).Date
# No need to pre-create an list object for your output - see below
$OUs=
#deleted for posting
$pwExpList = $OUs | ForEach-Object {
# Loop through all OUs
Get-ADUser -filter * -SearchBase $_ -Properties * | ForEach-Object {
# Loop through all found users
If (($_.PasswordLastSet -lt $CutOffDate) -and ($_.PasswordLastSet-gt $MaxCutOffDate) -and ($_.EmailAddress -notlike 'noreply*') -and ($_.EmailAddress -notlike '')) {
[PSCustomObject]@{
Email = $_.EmailAddress
PwExpires = $_.PasswordLastSet.AddDays(180)
}
}
}
}
# Output to csv
$pwExpList | Export-Csv -Path C:\temp\output.csv -NoTypeInformation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论