Solved

Append to an existing file in Powershell 2

Posted on 2014-11-01
4
125 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 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 69

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Synchronize a new Active Directory domain with an existing Office 365 tenant
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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