Reputation: 29
Hi guys im using this command to count Users inside a specific OU
$datum=get-date -Format MM-yyyy
$norm=(get-aduser -SearchBase "OU=Normal,OU=Benutzer und Computer,DC=my,DC=domain" -filter *|where {$_.enabled -eq "True"}).count
$spec=(get-aduser -SearchBase "OU=Special,OU=Benutzer und Computer,DC=my,DC=Domain" -filter *|where {$_.enabled -e "True"}).count
I mail myself the results monthly with Send-MailMessage
Now i want to save the results in a continuous Excel / CSV file. I found some faqs but i cant figure out how to export it the right way.
I would like to add the numbers i get it in 3 different colums automatically.
/ Date / Special / Normal /
Whats the best way to do it?
Upvotes: 0
Views: 26
Reputation: 3350
You can use a PSCustomObject to achieve it like this -
$datum = get-date -Format MM-yyyy
$norm =(get-aduser -SearchBase "OU=Normal,OU=Benutzer und Computer,DC=my,DC=domain" -filter *|where {$_.enabled -eq "True"}).count
$spec =(get-aduser -SearchBase "OU=Special,OU=Benutzer und Computer,DC=my,DC=Domain" -filter *|where {$_.enabled -e "True"}).count
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Date" -Value $datum
$obj | Add-Member -MemberType NoteProperty -Name "ADUserNormal" -Value $norm
$obj | Add-Member -MemberType NoteProperty -Name "ADUserSpecial" -Value $spec
$obj | Export-Csv $env:USERPROFILE\Desktop\File.csv -NoTypeInformation
Upvotes: 1
Reputation: 8432
Here is one way to do it:
[PsCustomObject]@{Date=$datum;Normal=$norm;Special=$spec} |
Export-Csv .\Data.csv -NoTypeInformation -Append
I assumed that by 'continuous' you meant that you were going to write to the same CSV over and over, hence the -Append
. Remove that if you want each attempt to over-write any existing data.
Upvotes: 1