Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Append to an existing file in Powershell 2

Posted on 2014-11-01
4
Medium Priority
?
411 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
2 Comments
 
LVL 41

Accepted Solution

by:
footech earned 2000 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 71

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

Technology Partners: 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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Loops Section Overview
Screencast - Getting to Know the Pipeline

772 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