Solved

Modifying Powershell Script to add date stamp to each record

Posted on 2014-02-07
8
604 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

729 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