namerg
asked on
How to append a csv file for logging instead of .log file
I have the following
$log = "C:\scripts\ceridian\05-22-2013_Ceridian_Create_AD_Users.log"
"SKIPPED - ALREADY EXISTS OR ERROR: " + $_."Last Name" + " " + $_."First Name" + " " + $_."Clock Number" | Out-File $log -append
Is there anyway to EXPORT/APPEND into a csv file ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Of course you need to take care to use the exact same format already in the CSV when perfoming appends. Neither the Export-CSV implemention of Dmitry Sotnikov does a check, nor the Add-Content; I don't know for the PS 3 one, but I reckon it's working the same way.
ASKER
@Subsun: I get the following in the generated CSV file: :)
I read only two languages no this one. :) :)
Created following users (on 05/28/2013 10:50:22):
-------------------------- ---------- --------
¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿/¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿/¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿/¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿-- ---------- ---------- ---------- --------
I read only two languages no this one. :) :)
Created following users (on 05/28/2013 10:50:22):
--------------------------
¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
Can you post the script which you are using?
ASKER
Here it goes:
Import-Module ActiveDirectory
# Get current directory and set import file in variable
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath = "C:\scripts\ceridian\05-22-2013_CeridianExport.csv"
# Define variables
#$log = "C:\scripts\ceridian\05-22-2013_Ceridian_Create_AD_Users.log"
$log = "C:\scripts\ceridian\05-28-2013_Ceridian_Create_AD_Users.csv"
$Out = "SKIPPED - ALREADY EXISTS OR ERROR:" + "," + $_."Last Name" + "," + $_."First Name" + "," + $_."Clock Number"
$date = Get-Date
$i = 0
# Change this to the location you want the users to be created in your AD
$location = "OU=New Users,DC=domain,DC=com"
# FUNCTIONS
Function createUsers
{
"Created following users (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append
Import-CSV $newpath | ForEach-Object {
$sam = $_."Last Name".ToLower() + $_."First Name".substring(0,1).ToLower()
$ClockNumber = $_."Clock Number"
Try {
#$exists = Get-ADUser -SearchBase "OU=ou,DC=domain,DC=com" -filter * -LDAPFilter "(sAMAccountName=$sam)"
$exists = Get-ADUser -SearchBase "OU=ou,DC=domain,DC=com" -filter * -Properties sAMAccountName,sn,givenName,title,employeeNumber | Where { $_.DistinguishedName -notmatch "OU=VIP" -and $_.DistinguishedName -notmatch "OU=DA" -and $_.sAMAccountName -notmatch $sam -and $_.employeeNumber -notmatch $ClockNumber} | Select-Object sAMAccountName,sn,givenName,title,employeeNumber
}
Catch { }
If(!$exists)
{
$i++
# Set all variables according to the table names in the Excel
# sheet / import CSV. The names can differ in every project, but
# if the names change, make sure to change it below as well.
$password = Get-RandomPassword
$domain = "@domain.com"
$setpass = ConvertTo-SecureString -AsPlainText $password -force
Write-Host $_."Last Name" , $_."First Name" $setpass
New-ADUser $sam -AccountPassword $setpass -OtherAttributes @{userPrincipalName=$sam + $domain; givenName=$_."First Name";sn=$_."Last Name"; displayName=$_."Last Name" + ", " + $_."First Name"; title=$_."Job Title"; employeeNumber=$_."Clock Number"}
$dn = (Get-ADUser $sam).DistinguishedName
#*****Set an ExtensionAttribute*******
#$ext = [ADSI]"LDAP://$dn"
#$ext.Put("extensionAttribute1", $_.ExtensionAttribute1)
#$ext.SetInfo()
# Move the user to the OU you set above. If you don't want to
# move the user(s) and just create them in the global Users
# OU, comment the string below
Move-ADObject -Identity $dn -TargetPath $location
# Rename the object to a good looking name (otherwise you see
# the 'ugly' shortened sAMAccountNames as a name in AD. This
# can't be set right away (as sAMAccountName) due to the 20
# character restriction
$newdn = (Get-ADUser $sam).DistinguishedName
$CN = $_."Last Name" + ", " + $_."First Name"
Rename-ADObject -Identity $newdn -NewName $CN
$output = $i.ToString() + ") Name: " + $CN + " sAMAccountName: "
$output += $sam + " Pass: " + $setpass
$output | Out-File $log -append
}
Else
{
#"SKIPPED - ALREADY EXISTS OR ERROR: " + $_."Last Name" + " " + $_."First Name" + " " + $_."Clock Number" | Out-File $log -append
Add-Content -Value $Out -Path $log
#"SKIPPED - ALREADY EXISTS OR ERROR: " + $_."Last Name" + " " + $_."First Name" + " " + $_."Clock Number" | Export-Csv "c:\scripts\ceridian\05-23-2013_ADMisMatch.csv" -NoTypeInformation
}
}
"----------------------------------------" + "`n" | Out-File $log -append
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes you are right about creating two files...
ASKER
Hmm, i get this:
Add-Content : Cannot bind parameter 'Encoding'. Cannot convert value "Default" to type "Microsoft.PowerShell.Comm ands.FileS ystemCmdle tProviderE ncoding" due to invalid enum
eration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Unknown, String, Unicode, Byte, BigEndianUnicode, UTF8,
UTF7, Ascii".
At C:\scripts\ceridian\Ceridi an_Create_ AD_Users.p s1:66 char:51
Add-Content : Cannot bind parameter 'Encoding'. Cannot convert value "Default" to type "Microsoft.PowerShell.Comm
eration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Unknown, String, Unicode, Byte, BigEndianUnicode, UTF8,
UTF7, Ascii".
At C:\scripts\ceridian\Ceridi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmm, now I get the following on the csv:
Column A
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
The original text/log file, creates the following:
Created following users (on 05/24/2013 16:46:43):
-------------------------- ---------- --------
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName(n) LastName(n) Clock Number(n)
I can use the txt/log file but for reporting purposes the CSV works great
Column A
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
SKIPPED - ALREADY EXISTS OR ERROR:
The original text/log file, creates the following:
Created following users (on 05/24/2013 16:46:43):
--------------------------
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName LastName Clock Number
SKIPPED - ALREADY EXISTS OR ERROR: FirstName(n) LastName(n) Clock Number(n)
I can use the txt/log file but for reporting purposes the CSV works great
ASKER
I got it I had to insert the following on line65 on a39202232
$Out = "SKIPPED - ALREADY EXISTS OR ERROR:" + "," + $_."Last Name" + "," + $_."First Name" + "," + $_."Clock Number"
ASKER
Not sure, I am all set on this one...but this is another stage on the code...i will let you know...
Here's a link to that.
http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/