[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-05-24
13
Medium Priority
?
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 41

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 2000 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 71

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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

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 2000 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

649 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