Solved

Append to an existing file in Powershell 2

Posted on 2014-11-01
4
163 Views
Last Modified: 2015-06-22
Hello.

I am trying to set up a simple logon script that will save user information on to some CSV files and it works like a charm in PowerShell V3 , but in Powershell V2 there is no "-Append" option to the CSV export function.

Is there any other way to do this in Powershell V2 ?
Thanks in advance.

Here is my script.


$UNC = "\\Server\Share\"
Date = Get-date -Format "dd-MM-yyyy"
$Time = (Get-Date).ToShortTimeString()
$NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
$ip = $NICInfo.IPAddress[0]
$SerialNumber = (gwmi Win32_BIOS | ? { $_.SerialNumber -ne $null }).serialnumber
$trmsrv = (Get-WmiObject -Namespace "root\CIMV2\TerminalServices" -Class "Win32_TerminalServiceSetting" -ComputerName $env:ComputerName).TerminalServerMode -eq "1"
$ClientName = If($trmsrv -eq "False"){""}If($trmsrv -eq "True"){$env:ClientName}
$Clientname1 = $env:CLIENTNAME
#$EventMessage = Get-EventLog -ComputerName "." -LogName 'Security' -newest 1 | Select-object Message
#$Event = If($EventMessage -match "on"){$Status = "LogOn"} Else {$Status = "LogOff"}

$Results = [PSCustomObject] @{Status = "LogOn"; Username = "$env:USERNAME"; Date = "$Date"; Time = "$Time"; ComputerName = "$env:ComputerName"; IPAddress = "$IP"; ClientName = "$ClientName1"; LogonServer = "$env:LOGONSERVER"}

$Results | Export-Csv "$UNC$(Get-Date -format `"MMyyy`").csv" -Delimiter ";" -NoTypeInformation -Append

$Results2 = [PSCustomObject] @{Status = "LogOn";Username = "$env:USERNAME"; PCSerialNumber = "$SerialNumber"; ComputerName = "$env:ComputerName"; IPAddress = "$IP"}

$Results2 | Export-Csv "\\FileServer\share\$env:USERNAME.csv" -Delimiter ";" -NoTypeInformation -Append
0
Comment
Question by:George Sas
[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
4 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40417648
Someone wrote a wrapper (proxy cmdlet) for Export-CSV to do this which you could try.
http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/
Otherwise, I suppose you could read in the .CSV file, then append the results to it in memory, then output the entire array again using the standard Export-CSV to overwrite the file.

BTW, pretty sure [PSCustomObject] didn't exist before PS 3.0 either.  I don't get an error when running in 2.0 mode, but the results aren't the same either.  You're probably going to want to use
New-Object PsObject -properties @{....}

Open in new window

to be compatible with PS 2.0.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40417691
Unless you have to support OS pre-W7, there is no reason to stay with PS2. If possible, you should install PS4 everywhere.

And footech is correct: In PS2, [PsCustomObject] will just treated as a hash table. Exporting it does not give the desired results (you are getting a hash table and meta-data only).
0

Featured Post

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

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
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.
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…
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…

710 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