[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

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

Open in new window

Is there anyway to EXPORT/APPEND into a csv file ?
0
namerg
Asked:
namerg
3 Solutions
 
footechCommented:
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
 
SubsunCommented:
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
 
QlemoC++ DeveloperCommented:
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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
namergSystems AdministratorAuthor Commented:
@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
 
SubsunCommented:
Can you post the script which you are using?
0
 
namergSystems AdministratorAuthor Commented:
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
 
SubsunCommented:
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
 
namergSystems AdministratorAuthor Commented:
yes you are right about creating two files...
0
 
namergSystems AdministratorAuthor Commented:
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
 
SubsunCommented:
PS3 support the Default enumeration for Encoding parameter.. I think you have PS2, try with UTF8
0
 
namergSystems AdministratorAuthor Commented:
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
 
namergSystems AdministratorAuthor Commented:
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
 
namergSystems AdministratorAuthor Commented:
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

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now