Solved

Modifying Powershell Script to add date stamp to each record

Posted on 2014-02-07
8
595 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
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…

777 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