Browsed by
Tag: Logging

How to: Automatically log your PowerShell session every time

How to: Automatically log your PowerShell session every time

Preface

At SharePoint Saturday New Hampshire I sat in a packed room and listened as Todd Klindt showed everyone how to install SharePoint 2013 without screwing it up (too badly). The big take away for me was this command that I had not heard about before called start-transcript. The power of start-transcript is that it is able to write everything that you do in a PowerShell session to a log file that you can review later. Todd demo’ed how he throws this every time he opens PowerShell and has found it to be invaluable.

The main reason that I had not heard of start-transcipt before is that I live in PowerShell ISE and rarely (if ever) go into plain ol’ PowerShell, and sadly start-transcript is not supported in PowerShell ISE. DAMN YOU POWERSHELL GODS FOR TEASING ME SO!!!!

The Use Case

The problem that this solves in my view is:

  1. I spend a ton of time tweaking away at some code on a SharePoint server, get it right and then inadvertently close the PowerShell window. It’s just GONE.
  2. Opening and closing PowerShell windows and trying to remember what I manually typed 2 hours ago to fix a problem that got reintroduced after redeploying code.
  3. Needing a way to review who made changes to the serverenvironment and see what they actually did.

The more I thought about it the more the more I liked the ability to log everything that is done in PowerShell on a server, but the issue that I had was that it was something that the person opening PowerShell had to remember to do every time they opened a window.

I started thinking about using PowerShell profiles to implement this for every user. In my experience I have seen profiles used infrequently, but in a couple of the scenarios they have been deployed via AD Group Policy. However if you are working in an environment that you don’t have access to create GPOs or you are working in a development environment and developing GPOs just isn’t your thing what do you do?

The Problem

The task at hand was two-fold:

  1. Auto deploy a PowerShell Profile for SharePoint Admins that would be lightweight, contain the start-transcript function to start automatically, assist in auditing, and be Server Administrator deployable
  2. Figure out if start-transcript like functionality was available for the native PowerShell ISE

The Solution

The solution that I came up with was to leverage the All Users Startup option in Windows to launch a script that would check to see if the folder that holds the PowerShell profile scripts exists. If it exists, the script terminates and all is well. This happens on every interactive login, but takes only a second. If the folder does not exist, the will kick off a creation of the scripts based upon a preset profile definition that includes the targeting & naming of the logs, starting the transcript, and loading the SharePoint module. The profile definition can be completely customized making this a viable approach for admins of other technologies, not just SharePoint.

For PowerShell this is great because it works out perfectly as built. PowerShell ISE on the other hand is still a thorn in our sides. The Scripting Guys, aka Ed Wilson and Craig Liebendorfer, wrote a terrific function that allows you to log the output pane in PowerShell ISE v1 & 2. Sadly this is not working in PowerShell v3 since there is no output pane. Leveraging that we can get part of the functionality in ISE that we get in the command-line version.

Behind the code

The solution that I came up with is in the form of a single PowerShell script that builds the following:

  • 2 folders (at the Root of C:)
    • c:PowerShellLogs will house all of the transcripts
    • c:PowerShellScripts will be the home for the scripts used to build the profiles and will be the default starting location when PowerShell opens. This way you can put all of the scripts you want to call in one place for all users and they can launch them easily.
  • check-profiles.lnk (in the All Users Start Menu Startup folder)
    • This shortcut points to a batch file in the C:PowerShellScripts folder called check-profiles.bat
  • check-profiles.bat (in c:PowerShellScripts)
    • This batch file launches the check-profiles.ps1
  • check-profiles.ps1 (in c:PowerShellScripts)
    • This PowerShell script checks to see if the user has a WindowsPowerShell folder in their My Documents folder. If it finds the folder, the script terminates. If it doesn’t find the folder it launches the create-profiles.ps1 script
  • create-profiles.ps1 (in c:PowerShellScripts)
    • This PowerShell Script creates the profiles for both PowerShell & PowerShell ISE.

Here is what I put in the PowerShell profile:

  • Set the location for PowerShell to start in to C:PowerShellScripts
  • Display a message about needing to Run as Administrator to effect changes
  • Set the path for logging the session to C:PowerShellLogs
  • Set the log name using the user context and date time stamp
  • Start the transcript
  • Display a message about waiting for the SharePoint snap-ins to load
  • Load the SharePoint snap-ins
  • Display a message that loading the SharePoint snap-in is complete and lets you know who you are running PowerShell as

Here is what I put in the PowerShell ISE profile:

  • Set the location for PowerShell to start in to C:PowerShellScripts
  • Display a message about needing to Run as Administrator to effect changes
  • Set the path for logging the session to C:PowerShellLogs
  • Load a function to set the log name using the user context and date time stamp
  • Load the function for Output-ISETranscript (the Scripting Guys code)
  • Display a message about waiting for the SharePoint snap-ins to load
  • Load the SharePoint snap-ins
  • Display a message that loading the SharePoint snap-in is complete and lets you know who you are running PowerShell as

Conclusion

While its may not be the perfect solution for PowerShell ISE that I was looking for when I set out, at the end of the day with this code I now have the ability to automatically log everything done in command line PowerShell.

Thanks to Todd Klindt, Evan Riser, Mark Rackley, and Dan Holme for talking through these use cases, reviewing some of the code, and taking differing view points on this to validate that it is a reasonable approach or not. I love being a part of a community where bouncing ideas and solutions off of peers is so easy and open.

You can find the code here:

powershell notepad

Hopefully this code will be useful to you in your day to day world.

Presenting “SharePoint Logging & Debugging: The Troubleshooter’s Best Friend” tonight at NYCSPUG on 3/7/2012

Presenting “SharePoint Logging & Debugging: The Troubleshooter’s Best Friend” tonight at NYCSPUG on 3/7/2012

If you are in the greater NYC area and are looking for a way to put an exclamation point on a great day of geeking out during the Virtual SQL Server 2012 launch, find your way over to the Microsoft offices at the Avenue of the Americas and come hear all about SharePoint Logging & Debugging. 

Here is the primer:

Troubleshooting and debugging issues in Enterprise SharePoint farms is a daunting task at best. SharePoint 2010 provides the tools to tackle the task, but not the roadmap. Come and join us as we lift the veil on the new and exciting features in SharePoint 2010, providing the roadmap to save you a ton of time every time you troubleshoot or debug an issue in SharePoint. Reclaim your evenings and weekends once and for all by taming the SharePoint debugging beast!

There will be food and drinks provided, and a SharePint following the event that promises to be a great time.  I have even heard that there will be some other out-of-towners making an appearance.

If all of that isn’t enough to rope you in, I will be giving away a Lync certified Plantronics Voyager PRO UC.

You can also check out my deck here: http://slidesha.re/wxIvNe

Hope to see you there!

spflogging_32

#SPSDenver Wrap-up

#SPSDenver Wrap-up

banner

I was honored to be a two-day speaker at SPSDenver this past weekend and had a marvelous time doing so.  The conference was well organized, properly planned, and extremely well attended.  Credit to Clayton Cobb and his team for putting on such a great event.  From the care of ensuring that they understood every issue that an attendee had to making sure that the fragile egos of the speakers were well cared for to making sure that every sponsor got the most out of the event, they really had their act together.  That was all before we even got to ShareSki!

Every time I travel for SharePoint I meet new, interesting people and it reinforces my opinion that the SharePoint Community is the greatest group of people with a common technology interest anywhere.  It is always fun to get to know more people in our world and I am humbled to be a part of it.

Needless to say, I was impressed by SPSDenver and am looking forward to returning next year if they are kind enough to have me back.

For those who are looking for the SPSDenver slide decks please find them below:

SharePoint Logging and Debugging

SharePoint Performance Best Practices from the Field

SharePoint Connections Las Vegas 2011 Wrap up

SharePoint Connections Las Vegas 2011 Wrap up

I just completed my 5 sessions, 3 of which I presented with my great friend Cornelius J. van Dyk, here at SharePoint Connections Las Vegas.  While here we received several pieces of great news:

1.) O’Reilly has green lit our book “SharePoint for Business Intelligence” which will be coming out in 2012 and will revolve around how you can solve business problems using SharePoint 2010, SQL Server 2012, & Visual Studio LightSwitch technologies for Business Intelligence.

2.) We will be doing our “SharePoint Disaster Avoidance for Large Scale Enterprises” post conference  and several other sessions for the SharePoint Connections Las Vegas show in March 2012

3.) Power was finally restored to my house in Nashua after more than 6 days!

I had almost 450 attendees across my 5 sessions and I sincerely hope that you all enjoyed the conference as my as I did.  As promised, here are links to my final decks:

SharePoint Logging & Debugging: The Troubleshooter’s Best Friend

SharePoint Performance: Best Practices from the Field

Battle Scarred But Still Standing: A SharePoint Administrator’s Tell All

Heavy Metal PowerPivot Redux

Human Workflow with Visio 2010 and SharePoint Designer 2010

Heading home tonight and then back to the SharePoint Road Trip on Thursday to hit SharePoint Saturday Denver!

SQL Server Default Trace

SQL Server Default Trace

In every version since SQL 2005 there has been a “background trace” enabled by default on all SQL Server installations.  This was a bit of a shocking revelation to me, as I am not a DBA, since every time I have called Microsoft dozens upon dozens of times and talked to tier 3 support / product team members for SQL Server and none of them have mentioned this fact when we have been diagnosing issues in SQL.  We have always had to set up a new trace.

Use the following command to validate that the default trace is enabled:

image

This will return something that looks like:

traceid

property

value

1 1 2
1 2 E:MSSQL10_50.instanceMSSQLLo​glog_560.trc  
1 3 20
1 4 NULL
1 5 1

From MSDN we get a legend that helps us under stand the above output:

Column name Data type Description

traceid

int

ID of the trace.

property int

Property of the trace:

1= Trace options. For more information, see @options in sp_trace_create (Transact-SQL).

2 = File name

3 = Max size

4 = Stop time

5 = Current trace status. 0 = stopped. 1 = running.

value sql_variant Information about the property of the trace specified.

The question I have heard most often since this discovery is “What does it matter?  Microsoft has it turned on by default so that must mean they intended it that way.  It must not cause a performance impact.  Right?”

After I stopped laughing at the idea that because Microsoft turned it on by default it must be ok (see Windows 2000 and before where everything was turned on and wide open upon install) I explained that the default trace is dropped into the installed Logs directory.  This cannot be changed.  You can stop the default trace and create your own, but that will break some of the built in reports that rely upon it.

The key is knowing that this is there.  While it may be lightweight and designed to be non-impactful, if you have anything else with a write intensive load directed at the same spindles you can expect to see some contention.

The positive of it is that now that we know it is there, it is extremely useful in troubleshooting.  The default trace can be queried by using the following command to pull back the most useful data (querying * pulls back WAY too much data for my taste):

image

Enjoy!

Our San Antonio SharePoint Connections Coast to Coast Boot Camp and Conference Slides

Our San Antonio SharePoint Connections Coast to Coast Boot Camp and Conference Slides

My good friend Cornelius J. van Dyk and I just completed our San Antonio, TX stop of our speaking circuit.  We promised we’d publish the slide decks from our Pre-Con Boot Camp and our three sessions at the SharePoint Connections Coast-to-Coast Tour.  Here you go!

SharePoint Connections Pre-Con Boot Camp

Wish Id Have Known That Sooner! SharePoint Insanity Demystified

Heavy Metal PowerPivot

Time is money.  How SharePoint Logging will save you both

We had a great time presenting, meeting everyone, sharing experiences and connecting with the San Antonio SharePoint Community at the “Ask the Experts” Session.  Thanks for the hospitality SATX!

Speaking at SharePoint Connections Coast to Coast 2011 in San Antonio

Speaking at SharePoint Connections Coast to Coast 2011 in San Antonio

SATX

My friend, Cornelius J. van Dyk, and I will be presenting at this year’s SharePoint Connections Coast to Coast conference in San Antonio held May 23-25 at the Hyatt Regency Hill County Resort & Spa.

We will be teaching the ITPro Pre-Conference “SharePoint Collaboration Bootcamp” plus presenting three sessions, “Heavy Metal PowerPivot”, “Time Is Money. How SharePoint Logging will Save You Both!”, and “Wish I’d Have Known That Sooner! SharePoint Insanity Demystified”. All 3 of these sessions promise to be fresh and exciting so come out and see us if you’re anywhere in the San Antonio area during that time.

We hope to see you there!

jase 

SPFLogger–A whole new way to look at SharePoint 2010 Logs

SPFLogger–A whole new way to look at SharePoint 2010 Logs

One of the most amazing, and little talked about, features of SharePoint 2010 is its new logging database and the power that comes along with it.  About 4 months ago I noticed a gap in the market and started talking to 2 friends of mine, David Feldman and Cornelius J. van Dyk, making the statement “I think that this would make for a really neat winforms app.”  After some brain storming we decided to take the idea a different direction and use Silverlight 4 RIA Services.

The tool in its initial beta offering is fairly simple, yet elegant.  It gives the user a very concise view of their NTEvent Logs, ULS Logs, and Timer Job Logs.  Rather than having to write T-SQL against the Logging Database’s views, we provide an easy to use Silverlight interface to quickly and easily view the log entries, complete with filtering and some basic analytics for NTEvent Logs and Timer Job Logs.

This was my first foray into development and coding, as almost all of my career in technology has been spent on the ServerOperationsArchitecture site of the world, and this was an eye opening and career life changing experience.  I got to work with 2 of my very good friends on a project that I am very passionate about, and I have been overly supported by my amazing, wonderful, beautiful, and ever patience wife, Jill, and my 2 amazing boys, Max & Sam, as I have dedicated my every waking free moment to this project.

Tonight, after 4 long months, I clicked the button to release SPFLogger to the CodePlex community.  Please go check it out and give us feedback so that we can continue to grow and evolve this into something great.  It is currently licensed under a dual license which allows for personal use under GPL version 2.0 and a custom license for commercial and closed source applications.  Basically we wanted to keep people from using our code to make money without giving some to us, but we want people to be able to use it!

The link is http://spflogger.codeplex.com.  We really hope that you love this tool as much as we do.

spflogger

Speaking at SharePoint Connections Coast to Coast 2011 in Boston

Speaking at SharePoint Connections Coast to Coast 2011 in Boston

image

My friend, Cornelius J. van Dyk, and I will be presenting at this year’s SharePoint Connections Coast to Coast conference in Boston held April 25-27 at the Marriott Long Wharf.

We will be presenting two sessions, “Heavy Metal PowerPivot” and “SharePoint Logging – The Undiscovered Country”. Both sessions promise to be fresh and exciting so come out and see us if you’re anywhere in the Boston area during that time.

We hope to see you there!

 imageimage