Add content to a csv

Hello, i was searching through Q&A and didnt find anything in 20 pages.

The last part of my script need to creat a log file with my users informations and i did it without problem but when i delet the log file and start over my script i got all the data that i add before deleting this file.

 

Here is my code :

[pre]

foreach ($user in $notifyUsers)
{
$Name = $user.Name
$details = @{
Date = get-date
Nom = $user.Name
JourAvantExpire = “$daysToExpire jour(s)”

}
$results += New-Object PSObject -Property $details
}
$path = “C:\temp\MotdePasseLOG.csv”
If(!(test-path $path))
{
New-Item -path C:\temp\ -Name LOG.csv -Value $results
Write-Output “Création du fichier log”
$results | export-csv -Path C:\temp\LOG.csv -NoTypeInformation
}
else
{

Add-Content -path “C:\temp\LOG.csv” -Value $results
Write-Output "Fichier existant mise à jour
"
$results | export-csv -Path C:\temp\LOG.csv -NoTypeInformation
}

[/pre]

 

Please format your code as code using the code tag button (“pre”) on the edit bar of the post editor. Thanks.

If you want to append some new data to your existing file you have to use the parameter -Append.

Thank but do you have some exemple of the use of -Append ? I only find the Add-Content when i search for it.

Get-Help Export-Csv -Parameter Append

Should get you started. :slight_smile:

I recommend for you to ALWAYS read the COMPLETE help for the cmdlets you use including the examples.

Export-CSV
:wink:

Yeah sorry, i didnt even think about it! Thank you all!

Hello, i just want to thanks you all, my script is done and it’s working well!

All is fine, do not hesiste to ask me if needed

[pre]

$start = [datetime]::Now
$date = get-date
$textEncoding = [System.Text.Encoding]::UTF8
$today = $start
$exclude = “HealthMailbox*”,“SystemMailbox*”,“AdmDyn*”
$defaultMaxPasswordAge = (Get-ADDefaultDomainPasswordPolicy -ErrorAction Stop).MaxPasswordAge.Days
$users = get-aduser -filter {(Name -notlike “Migration*”) -and (Name -notlike “DiscoverySearchMailbox*”) -and (Name -notlike “On Premise*”) -and (Name -notlike “MSOL*”) -and (Name -notlike “Federated*”) -and (Name -notlike “Exchange*”) -and (Name -notlike “HealthMailBox*”)-and (Name -notlike “SystemMailbox*”) -and (Name -notlike “AdmDyn*”)} -properties Name, PasswordExpired, PasswordLastSet
$results = @()
Write-Output “La durée de vie d’un mot de passe sur le domaine est de $defaultMaxPasswordAge jours”

#Appliquer à chaque utilisateur:
foreach ($user in $users)
{
$Name = $user.Name
$PasswordLastSet = $user.PasswordLastSet
$SamAccountName = $user.SamAccountName
$MaxPasswordAge = $defaultMaxPasswordAge

$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge.Days
}
$userObj = New-Object System.Object
$ExpiresOn = $PasswordLastSet.AddDays($MaxPasswordAge)
$fichier = “C:\temp\Log.csv”
$daysToExpire = New-TimeSpan -Start $today -End $Expireson
$daysToExpire = [math]::Round($daysToExpire.TotalDays)
$userObj | Add-Member -Type NoteProperty -Name UserName -Value $SamAccountName
$userObj | Add-Member -Type NoteProperty -Name Name -Value $Name
$userObj | Add-Member -Type NoteProperty -Name PasswordSet -Value $PasswordLastSet
$userObj | Add-Member -Type NoteProperty -Name DaysToExpire -Value $daysToExpire
$userObj | Add-Member -Type NoteProperty -Name ExpiresOn -Value $ExpiresOn
$Name = $user.Name
$details = @{
Date = get-date
Nom = $user.Name
DernièreModifMotDePasse = “$PasswordLastSet”
JourExpiration = $ExpiresOn
NbDeJoursAvantExpiration = “$daysToExpire jour(s)”

}
$results += New-Object PSObject -Property $details
}
if (!(Test-Path $fichier))
{
$results | export-csv $fichier -NoTypeInformation
Write-Output “Log créé.”
}
else
{
Add-Content $fichier -Value $results
Write-Output “Log mise à jour.”
}
Get-ChildItem "C:\temp" -Recurse | Where CreationTime -lt (Get-Date).AddDays(-365) | Remove-Item -Recurse

[/pre]

Very nicely done indeed!

I thought it could use a little tweaking, I saw a few points where things are created but never used, and some methods that could be improved a little bit. Looks pretty solid already though. :slight_smile:

This is what I came up with after a bit of tinkering:

 

Thank you Joel ! You clean this and it looks so great now ! Thank you again ! ! !