Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Modifying Powershell Script to add date stamp to each record

Posted on 2014-02-07
8
Medium Priority
?
621 Views
Last Modified: 2014-02-13
I am using a PowerShell script to create a report of Citrix 6.5 user sessions.  This is how the report looks.  Script is running on a windows 2008 R2 server.  

2/7/2014 3:31:40 PM
AccountName   ServerName    ClientName   BrowserName ClientIPV4   SessionName
-----------   ----------    ----------   -----------  ----------   -----------
Domain\charlie    CTX08                PC4            Publish App 1                           RDP-Tcp#1  
Domain\mike        CTX02               PC1             Publish App 1                          ICA-TCP#22
Domain\jerry        CTX07               PC3             Publish App 3                           ICA-TCP#6

Need help modifying the script to add a date/time stamp to each record.
Here is an example.
Date Time             AccountName   ServerName    ClientName   BrowserName  ClientIPV4   SessionName
-----------   ----------    ----------   -----------  ----------   -----------
2/7/2014 3:31:40 PM   Domain\mike        CTX02               PC1             Publish App 1                          ICA-TCP#22
2/7/2014 3:31:40 PM   Domain\jerry        CTX07               PC3             Publish App 3                           ICA-TCP#6
2/7/2014 3:31:40 PM   Domain\charlie    CTX08                PC4             Publish App 1                           RDP-Tcp#1  


This is the PowerShell script
add-pssnapin citrix* -erroraction silentlycontinue
write-host (get-date) 
get-xasession -farm | select-object -property AccountName,ServerName,ClientName,BrowserName,ClientIPV4,SessionName | ft

Open in new window


Note that the IP address of the client is not returned.  It is not known to me why it does not return the  Client's IP.  If the IP address fix is easy-peasy please suggest one.  This question’s priority is adding a date time stamp to each record.
0
Comment
Question by:epmmis
[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
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39843028
Create a custom property to add date..
Get-xasession -farm | select-object -property @{N="Date Time";E={Get-date}},AccountName,ServerName,ClientName,BrowserName,ClientIPV4,SessionName | ft

Open in new window

0
 

Author Comment

by:epmmis
ID: 39846610
Good answer.  almost got it.  I could not tell by the time was included with the date.

Below is the resulting report.  Need 2 modificatons to make it right.
1.  Allow the entire field to be displayed.
2.  Loose the header info.  I tried | ft -HideTableHeaders

Date Time   AccountName ServerName  ClientName BrowserNam ClientIPV4 SessionNam
                                               e                     e        
---------   ----------- ----------  ---------- ---------- ---------- ----------
2/10/201... DOMA\Chi... WINCTX06    PC12345    DOMA De...            ICA-TCP#0
2/10/201... DOMA\Chi... CTXZDC1     PC12345                          RDP-Tcp#0
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39846623
You can us FT -Auto to display entire data.. also if you are trying to export the output to a file then better use Export-csv to export the details to csv file.
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:epmmis
ID: 39846626
I almost figured it out.   I appended this to the code -
| Format-Table  -HideTableHeaders  -autosize

Got this out put

2/10/2014 2:49:00 AM
WARNING: column "SessionName" does not fit into the display and was removed.

2/10/2014 2:49:01 AM DOMAIN\Chil   CITRIX06   PC12345      DOMAIN Desktop        
2/10/2014 2:49:01 AM DOMAIN\Chil   CTXZDC1    PC12345                          

Is it possible to get the last property "SessionName" on the report?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 39846754
It's because your PowerShell console window width.. you need to increase the console window width to fit more columns..

Like I said, you can export the report to csv file and view it in excel..

Get-xasession -farm | select-object -property @{N="Date Time";E={Get-date}},AccountName,ServerName,ClientName,BrowserName,ClientIPV4,SessionName | Export-csv C:\report.csv -nti

Open in new window

0
 

Author Comment

by:epmmis
ID: 39850692
Can the export be configured to append the data?
The data will be used to determine who was logged on and from where.
0
 

Author Closing Comment

by:epmmis
ID: 39856553
Thank you for your help.  i did learn that the csv export does not append.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39856920
With PowerShell Version 3.0 and above you can use -Append switch with Export-csv..

Try the following code if you have a lower version of PowerShell.
Get-xasession -farm | select-object -property @{N="Date Time";E={Get-date}},AccountName,ServerName,ClientName,BrowserName,ClientIPV4,SessionName | ConvertTo-Csv -nti | Select -skip 1 | Out-File C:\report.csv -Encoding ASCII -Append

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
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…

636 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