Solved

Modifying Powershell Script to add date stamp to each record

Posted on 2014-02-07
8
588 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
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:epmmis
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your help.  i did learn that the csv export does not append.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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

Wish Marketing would stop bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now