Solved

Modifying Powershell Script to add date stamp to each record

Posted on 2014-02-07
8
603 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

697 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