Solved

How to append a csv file for logging instead of .log file

Posted on 2013-05-24
13
438 Views
Last Modified: 2013-06-06
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

Open in new window

Is there anyway to EXPORT/APPEND into a csv file ?
0
Comment
Question by:namerg
13 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39195824
With PowerShell 3.0, Export-CSV has an -append parameter.  But if you can't use that, I've come across a function someone put together to do that.  It's basically a wrapper for Export-CSV that adds the capability.
Here's a link to that.
http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39195835
You can try constructing the csv file..
For example..
$log = "C:\scripts\ceridian\05-22-2013_Ceridian_Create_AD_Users.csv"
$Out = "SKIPPED - ALREADY EXISTS OR ERROR:" + "," + $_."Last Name" + "," + $_."First Name" + "," + $_."Clock Number"
Add-Content -Value $Out -Path $log

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39196978
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.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:namerg
ID: 39202148
@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):
--------------------------------------------
¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿/¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿----------------------------------------
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39202203
Can you post the script which you are using?
0
 

Author Comment

by:namerg
ID: 39202232
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

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39202346
I can see in the script you have used Add-Content and Out-File. One thing you can try is to keep the same encoding while updating the file and see if you can get a good output..

Add   -Encoding UTF8  or   -Encoding Default while updating the files..

For example..
Add-Content -Value $Out -Path $log -Encoding Default
"----------------------------------------" + "`n" | Out-File $log -append -Encoding Default

Open in new window


Also I would suggest separate log files for created user and failed users so that the csv file will be more logical. Right now it's looks a bit vague as also it doesn't have any header..
0
 

Author Comment

by:namerg
ID: 39202377
yes you are right about creating two files...
0
 

Author Comment

by:namerg
ID: 39202388
Hmm, i get this:
Add-Content : Cannot bind parameter 'Encoding'. Cannot convert value "Default" to type "Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding" 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\Ceridian_Create_AD_Users.ps1:66 char:51
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39202414
PS3 support the Default enumeration for Encoding parameter.. I think you have PS2, try with UTF8
0
 

Author Comment

by:namerg
ID: 39202895
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
0
 

Author Comment

by:namerg
ID: 39202913
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"

Open in new window

0
 

Author Comment

by:namerg
ID: 39203060
Not sure, I am all set on this one...but this is another stage on the code...i will let you know...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question