Solved

Append to an existing file in Powershell 2

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

13 Experts available now in Live!

Get 1:1 Help Now