PowerShell – Investigating Slow Startup via reviewing Auto-loaded System Modules & that bad disk

Introduction

Here is another in a series of articles that recounts my experience a few weeks back.  I had mysteriously broken my SharePoint 2010 Foundation Server fresh install.  I think SharePoint only stayed up for 4 to 6 hours, before I broke it.

Experience with SharePoint

Yes, I have installed SharePoint a few times over the last years.  But, never could fully understand\idolize it.  And, so to put it nicely, I do not know my way around it.

Once Broken

Once broken, I looked for ways of Lazarus’ing it.  One of the bright ideas that came over the Net was to try using “SharePoint Diagnostics Studio 2010” to bring it back up.  But, as my server is running MS Windows 2012, SharePoint 2010 had interoperability problems with it.  The specific problem has do with PowerShell v3 vs v2.

Actual Brokenness

One of the things my Internet friends suggested was to have SharePoint auto-load the system modules.  So you know me, I looked on the Net and found ways to make that happen.

Here are some sample codes:

Starting Windows PowerShell with Import System Modules (2.0)
http://technet.microsoft.com/en-us/library/hh847866.aspx

Get-Module -ListAvailable | Import-Module


How to import system modules automatically
http://stackoverflow.com/questions/10027156/how-to-import-system-modules-automatically

Get-Module -ListAvailable | Where-Object {$_.Path -like "$PSHOME*"} | Import-Module

But, as the story goes, you have to do this each time you run a PowerShell session.  And, so next in line was to change my PowerShell Profile; as doing so will allow my changes to auto-run per each session.

This was important for what I was doing.  PowerShell was being invoked my SharePoint diagnostics and so I needed the changes to happen transparently; as I will not be able to step in and invoke beforehand.

There are good documentation along this lines:

But, as my specific problem was not solved (via that path), I will selfishly avoid re-countering my experience.

Slow PowerShell

So let us go back to PowerShell…  Once my machine was verifiable broken, I had so much work to do.  A few weeks later, I am still Spring Cleaning.

In this post, I will like to tackle slow PowerShell sessions; specifically upon start-up as I do not spend that much time within PowerShell to know whether PowerShell is slow during usage, as well.

Metrics

I can hear you say “what you talkin’ bout willis“…

So here is my proof:

Using a very nice and free tool courtesy of Gammadyne ( http://www.gammadyne.com/cmdline.htm#timer), we are able to time how long it takes PowerShell to arise, spit out a line, and exit.

BTW the tool is named timer.exe and here is our test script:

rem Start Timer
timer /nologo

powershell.exe -Command "Write-Host 'In PowerShell' "

rem Stop Timer
timer /s /nologo

Output:

Time Taken

elapsed

Memory Utilization:

memoryUtlization

One thing I have to own up to is that you might not notice the slowness when starting PowerShell, but upon running your first command, you will notice the slowness.

And, so I will assume that the system modules are not loaded during initial start-up, but during actual command processing.

The other thing you will notice from the memory Utilization screen above, is that the memory Utilization is @ 8 MB post initial start-up (PID = 9028).  But, once I issue my first command, it climbed — You will notice that climb if you track PID = 9028 on the screen below and compare to the screen shot above.

memoryUtlization-postInitialCommand

Visibly from our screen shot above, memory was originally at 8912 K (8 MB), but now it is 28,156K (28 MB).

Review the list of auto-loaded System Modules


Get-Module -ListAvailable | Where-Object {$_.Path -like "$PSHOME*"}</code></pre>

Output:

listAutoloadedSystemModules_v2

Unload auto-loaded System Modules (Current Session)

For our current PowerShell Session, let us remove all loaded PowerShell Sessions

Get-Module -ListAvailable | Where-Object {$_.Path -like "$PSHOME*"} | Import-Module

Output:

removeModule

Please keep in mind that PowerShell does not release used-memory back to the OS, post removing these modules.

Review loaded Modules (Post System Module Unloading)
 

 Get-Module

Output:

Get-Module (post system module unloading)

Review User and System Profiles

Though I promised not to talk about User & System Profiles, it will be unfair not to do so.

So a quick note: PowerShell is a total user experience, as such, it allows plenty of customization and automation.  Do you want your user customize a certain way, do you want the entire system to a tailored a certain way…etc.. whatever.

So my point is that via Group Policies, Active Directory, user customization, your system might be configured by you or others to experience or for better supportability.

And, so please review some of those over-aching configurations, as well.

For our present discussion, as this is in my LAB Environment and I know what I did, let us simply review system and user profiles for this one MS Windows 2012 server.

Courtesy of Ed Wilson, Windows Scripting Guy:

Understanding and Using PowerShell Profiles
http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/04/understanding-and-using-powershell-profiles.aspx

 

So Ed says there at a couple of Environment variables and consequent folders and files that we want to look for; and those are $Host (Current User) and $PSHOME (all users and current system.

 

Environment Variable Base Path Relative Path and File name
$Host C:\Users\<username> Documents\WindowsPowerShell\Profile.ps1
$PSHOME C:\Windows\System32\WindowsPowerShell\v1.0 Microsoft.PowerShell_profile.ps1

So on our MS Windows 2012 Test system, our files will be named:

  • User Profile :- C:\Users\dadeniji\Documents\WindowsPowerShell\Profile.ps1
  • All Users and System :- C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

Again, please review presence and contents of these files.

Exited PowerShell and Restarted

So I am all giddy and excited.  Quickly exited PowerShell and restarted it.  Upon restart, noticed the same heaviness.

And, so I am thinking may be it is not want I did after all.  Took to the Internet and looked for help.

Checking for Profiles again

Jeffery Hicks has an express way for checking for these files:

Jeffery Hicks, PowerShell MVP
http://www.minasi.com/forum/topic.asp?TOPIC_ID=39253


$profile.CurrentUserAllHosts, $profile.CurrentUserCurrentHost, $profile.AllUsersAllHosts, $profile.AllUsersCurrentHost | test-path

Output:

checkProfile

So thankfully, Jeff’s code came back “clean as whistle”.

Checking Internet Options

InternetOptions
The specific options that we want to review are:

  • Check for publisher’s certificate revocation
  • Check for server certification revocation*

As the items listed above causes the system to initiate Internet connections and wait for response, they can cause network latency issues.  And, so consider “temporarily” un-checking them.

If I were you, I will run netstat and see if any network requests are stuck on SYN_SENT:

netstat -an | find "SYN_SENT"

SysInternals – Process Monitor

Came back and tried SysInternals Process Monitor to see if I can catch the powershell.exe getting stuck at an obvious spot.

Nothing.

MS Windows Event Viewer

Next checked MS Windows Event Viewer.  Usual errors for this machine as one of the physical hard-drives is bad and unfortunately I never “totally” removed it.  And, so I am seeing errors such as:

  • Log Name :- System
    Source :- Disk
    Event ID :- 153
    Details :- The IO Operation at logical block address for Disk was retried

But, also noticed that Event Viewer was very slow.

Slow Event Viewer

I am a bit familiar with slow Event Viewer.  And, posted about it previously in “Microsoft – Event Viewer – Error – This snap-in is not responding” https://danieladeniji.wordpress.com/2012/11/01/microsoft-event-viewer-error-this-snap-in-is-not-responding/.

Microsoft .Net

The fix was basically to re-install Microsoft.Net.  But, I have already done that recently. And, so did not want to go back down that line.

Microsoft .Net – Framework Repair Tool

I am also familiar with Microsoft .Net Repair Tool;  The original one and the new one.

Microsoft is being very liberal in how it exposes and productize’s this repair tool.

The online documentation states that this is v1.0 tool and that it was released on 10/18/2012:

MicrosoftNetRepair-Details

But, once downloaded an ‘aware’ mind will show:

MicrosoftNetRepair

that the file’s version is 4.5.x. matching a current Microsoft .Net version.

So it appears that Microsoft wants to you have the latest code and they will stamp out the old ones.

Microsoft .Net – Framework Repair Tool – Trial Run

Tried running the repair tool, but it did not come up.

Breakfast

Already promised to take parents to breakfast.  It is @ the First Street Ale’s house (http://www.firststreetalehouse.com/the-food/) in Livermore, CA.

They have a breakfast buffet on Saturdays and Sundays.  And, you really can not afford to miss it, as it is something you need more of in your LIFE.

Remove Disk

Brought out the old screw drivers and removed the failing disk

Fix Windows Boot Manager

The machine has also been coming up with error messages that suggests that my Boot Partition/Manager is beaten.

Error Messages includes:

BIOS Errors

  • SATA Port 1 ST3750528AS CC45
    S.M.A.R.T Capable and Status BAD
  • AHCI Port1 Device Error

MS Windows Boot Error

  • The boot configuration data for your PC is missing or contains errors
  • 0xc00000f
  • \boot\bcd

So let us go correct that.  The best help, as always, comes from the Internet via Google:

Depending on your OS, one of this two fine postings will help.

Here are the steps:

  • Get a Windows Install CD/DVD that supports your OS
  • My OS is MS Window 2012, but could not find the CD/DVD
  • And, so tried MS Windows 2008/R2, but it was missing BCDBoot.exe
  • Since I have MS Windows 7, tried that one and it has BCDBoot.exe
  • And, so used it and made my way to the DOS Command Prompt

Let us confirm the drive letter for our system volume.  To do so, access DOS and
run diskpart.  Once in diskpart, issue “list volume”:

Syntax/Sample:

list volume

  Screen Shot:

diskpart-listVolume
From the screen shot, we can confirm that our system disk/volume is Drive C:; and as a bonus it is marked as healthy.

Next up is to issue bcdboot.exe and pass in my system boot partition.

Sample:

x:\Windows\System32 <b>bcdboot c:\windows /s c: </b>

As a quick explanation:

  • X: is the CD/DVD Drive that has my OS Install CD/DVD
  • \Windows\System32 is the location where bcdboot is located.  Your experience might be different and so I will suggest that you sojourn to your root folder and issue “dir bcdboot.exe /s”
  • Windows is installed on Drive C:
  • Windows install path is C:\Windows
  • /s means to include “System Partition”
  • The target drive is C:

Reboot

Once we changed the boot Information, we should restart the machine.

Re-Check PowerShell

elapsed - fix

Wow! Powershell now came up within 1 sec.  Consequent checks shows PowerShell starting up within 5 seconds.

Conclusion

It is difficult to say that I have added anything to the discussions.  But, I will say pay attention to your error logs; Event-Viewer in this case – The system and Application branches.

Document your changes and see patterns.

And, lastly hopeful someone you trust such as a Scott Hanselman blogged about it. And, so it is worth you following up.

 

References

References – System Modules Management

References – PowerShell Start-up

References – Bad Disk

 

References – BCDBoot

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s