Solved

Append to an existing file in Powershell 2

Posted on 2014-11-01
4
105 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
4 Comments
 
LVL 39

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 68

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

Problems using Powershell and Active Directory?

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now