Wednesday 13 February 2013

Reboot and Resume and RunOnce PowerShell Script

Overview:  It is fairly common to need a reboot and you want to automatically run some PowerShell once the server is back up.

This package contains 2 PS 1 files (download):
  • Restart-VM.ps1 (Contains all the code to reboot the server and lainch the function from here)
  • Restart-VM-Custom.ps1  (2 functions to add your custom logic.  You can add either to the Pre-shut down code to execute or the PS to run once the server has restarted)
Steps
  1. Extract both PS1 files into the same folder on your machine;
  2. Edit the file "Restart-VM-Custom.ps1", noteably create your custom scripting logic within the "CustomRestartActions" function; and
  3. Run "Restart-VM.ps1" as the administrator.
PS to reboot remote machines:
PS> Restart-Computer -computer 192.168.1.2 -force

RunOnce Example (Download)

1 PS files: 
1st file changes the registry to autologgon and run the runonce.


2nd ps file creates a folder, removes the autologin and reboots

Remote RunOnce using PS, useful for remote builds (Download)

Run on build server to setup RunOnce on the remote server
 Run on the target Remote Machine


Remote PS using Invoke-Remote
 
Updated 10 June 2013: I am using a REG_EXPAND_SZ Registry key type and I have discovered that the value needs to be less than 255 characters.  Mine was 270 odd characters as it had multiple attributes.  E.g.
RunOnce>c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command " .'C:\Windows\Temp\SQLInstall\Start-ServerCustomisation-SQL-All.ps1' 'k:\SQLInstallerInput-Test.xml' 'k:\\192.168.1.199\c$\LocationNotingDeployment\vSphere\Test /user:xxxxxxxxxxxxxxxxxxxxxxx P@aassssss'"</RunOnce>

My runonce was not firing, however the RunOnce registry key was saved correctly (270 chars).  The command could be executed from the cmd prompt with the longer length but to automate the Reg-Expand_SZ key value needs to be less than 255 characters long.

Friday 1 February 2013

PS functionality - logging, xml, local permissions, remote PS

Read config values from an xml file - download zipped files

Download contains:
PS-Logging.Ps1 - Allows for an optional paramter to set the XML file to read from. Of the input xml is not valid or specified revert to reading a predefined xml file from the same location in the directory that PS-logging.PS1 is saved to.
PS-LoggingMore.Ps1 - Easy way to log the PowerShell consoles actions. As it doesn't work with PowerGui, it will check the IDE.
AutoInput.xml - Is an xml files that is used for configuration of these examples.
PS-Logging.png - describes the workings of the files (ignore).

Other:
Import External Functions (download) - ability to write separate PS files and call the functions within from the main/starting PS file.

Specifying Input Parameters Strict Example:
param(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
$configLocation
)

**************************************************

Ability to Execute PS on a remote trusted computer

The PS below has 2 simple functions to illustrate:
  • running a remote PS commands (tip, ensure the PS window is running using a network account that has administrator rights on the 2 remote servers) and
  • adding a domain user account or AD group to the local machine administrators group.


Invoke-Command -computername sp-srch1, sp-srch2 -command {
 $computer = [ADSI]("WinNT://" + $env:COMPUTERNAME + ",computer")
 $Group = $computer.psbase.children.find("administrators")
 $acc = "demo/sp_searchservice"
 $Group.Add("WinNT://" + $acc)
 Restart-Service -Name SPSearchHostController  # Restart the windows service
 Restart-Service -Name OSearch15
}

Wednesday 30 January 2013

Useful PowerCli Snippets

Title: Revert to a specific set of snapshots
Description:
  • Ps that accepts a configuration file and the name of the snapshot to revert to. 
  • Xml holds the config values to connect to vSphere. 
  • Add the PowerCli PS add-in.
  • Connect to vSphere
  • Loop thru the VM's and revert to a specific snapshot.

    *****************

Title: Create a snapshot of multiple VM's

 
 
   *****************


Tuesday 8 January 2013

Create SQL Server Aliases using Powershell


Create SQL Aliases example Powershell
For DR and Moving/splitting up SQL Server load use aliases, costs you nothing and later on you can split the load.  I use 3-4 even on small SP farms.

Tip: SQL 2012 has always on availability clustering, the SQL Server listener (need for Availability Groups (AG)) does the same functions as a SQL Alias.  So my take is if you use a SQL 2012 AG then the listener on an always on availability cluster does the same function as the SQL Alias.  Obviously rather use the listeners DNS name as opposed to the IP adr of the listener but if you are using AG you don't need a SQL Alias.

Thoughts: SQL 2012 brings a new option to the table regarding SQL Aliases for SP2010 & SP2013.  If you are using Always-on Availability Groups (AP) in SQL 2013, you get a SQL listener that does the same function as as the SQL Alias.  AG gives you automatic db fail over for your Sp farm.  Issue is if you use AG with a SQL alias you have a single point of failure so your DB won't automatically fail over.

So the big reason to use SQL Aliases for me in the past was to allow me to split my database servers when 1 became the bottleneck.  The goodness with AG outweighs this option to improve performance especially as if I'm using AG I probably have sufficient resources as this is planned upfront.

Creating Registry keys safely in PowerShell:
    # Check if the key already exists - Example from AutoSPInstaller on creating aliases.   
    $client = Get-Item 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client' -ErrorAction SilentlyContinue
    # Create the key in case it doesn't yet exist
    If (!$client) {$client = New-Item 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client' -Force}



Tip:  Check SQL connections and SQL Aliases using a udl file.  Create a text file on your desktop, rename the .txt extension to .udl.  Open the UDL file and verify the connection string works.  I check the Alias that uses the AOAG listener, if this fails I check the connection using the listener, if this fails I check I can hook to any SQL instance.  This pretty much tells me where I have gone wrong.

Tip:  Review your SQL Alaises and cleintside neworking using the SQL Server Client Network Utility tool.  In the run window type: cliconfg

Monday 24 December 2012

Digital Signatures and Install Software gotcha

Problem: In automating SQL Server and SharePoint images, the actual installation is taking a long time on my managed environment whereas my developer laptop is fast.  All installations are done without Internet access (offline).

I have a dev environment build on my laptop that runs SSD and I run 3 VM using VMware workstation 9 (all use Windows 2008 R2 SP1).  I create an 1) AD with 1GB or RAM and 1 CPU 2) SQL 20012 with 10GB RAM and 4 CPU's 3) SP2010 CU Aug 2012 10GB 4 CPU's.  All the installation is automated using slip streamed images.

So for simplicity on the CI environment I will explain a simplified comparable setup. I have 3 machines with the same roles however the SQL 2012 and SP2010 install take considerably longer.  The CI environment is on ESX (Cisco blades & chassis, and Violin (SSD) storage.  The CPU/compute is connected to the storage via SAS/Fibre channel made no difference either).  I have summarised the results below:

                                                                 SQL2012 (duration)      SP2010 (duration)
Laptop(VMworkstation Workstation)           15 min                              16 min
CI (ESX)                                                        22 min                              92 min 

Finding: My hardcore/good ESX infrastructure is taking 9 minutes longer to install SQL Server 2012 on beter hardware and an amazing 70 minutes longer to install SP2010.

Update 21 Feb 2013: Don't use PowerShell 3 with AutoSPInstaller (including using the version switch i.e. -version 2), it doesn't work and even changing AutoSPInstallers internal web call fail.  It can be made to work with the version 2 switch but it isn't worth the effort.

Initial Hypothesis:
After many many hours between service providers managing the infrastructure, it was not hardware or ESX configuration/setup.  However if the network card on the VM is disabled, the performance change improves to:

                                                          SQL2012 (duration)            SP2010 (duration)
CI (ESX)                                                     13 min                       5 min and 5 seconds

Pretty hefty improvement.  Using netstat is looks like there are requests to the Internet.  After adding Wireshark to monitor all traffic.  I can see requests being sent to crl.mirosoft.com (certificate revocation lists) and ctldl.windowsupdate.com

Issue shown in Wireshark
Issue Shown in Fiddler
This is the 1st time I have seen this issue in a clients production environment.  If the WFEs/SP servers have internet access (less preferable) or the servers don't have access the install work in a timely fashion.  The symtoms of the issue are when the WFE's/SP Servers don't have internet access but think they do.  All the binaries are digitally signed and the install will try validate the signatures despite this being an offline install.

I confirmed the problem being how the networking is setup.  My issue shows up on the VM NIC adapter, Originally the IPv4 Connectivity has a status of "No Internet Access", once I ping google I get a reply and the status changes to "Internet".  I can ping google but not browse to it.


Resolution:  The problem is that executable code is digitally signed.  This is good, all code should be digitally signed so it can be authenticated.  However in this situation a lot of requests are being sent out from the VM as the install tries to verify all the SharePoint complied code.  The install on the local VM acts as if there is an Internet connection (which there is not).

It takes unique networking to get into this issue and SP/any digitally signed code will check the digital certs.

There are a few fixes such as:
1.> Allowing the servers to get out to the Internet, so open the firewall or set a proxy on the local VM.
2.> Add host entries to the cert fails immediately but will continue installing (This is not working for me).
3.> Make the following registry change:
set-ItemProperty -path "HKCU:\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing" -name State -value 146944
set-ItemProperty -path "REGISTRY::\HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing" -name State -value 146944
get-ChildItem REGISTRY::HKEY_USERS | foreach-object {set-ItemProperty -ErrorAction silentlycontinue -path ($_.Name + "\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing") -name State -value 146944}


More Information:

Certificates for installing sofware is cause slow install:
http://joelblogs.co.uk/2011/09/20/certificate-revocation-list-check-and-sharepoint-2010-without-an-internet-connection/

http://ddkonline.blogspot.co.uk/2010/05/fix-sharepoint-very-slow-to-start-after.html

If you want to verify if a machine is having problems with a poarticlar process Process Explorer (Usefule if a machine has high memory, CPU or IO issues)

Sunday 9 December 2012

SharePoint Saturday UK 2012

Great event again, it amazes me that such a great place to get info meet people learn about SharePoint attracts 250 people - that is free.  Sure you need to give up your time and it is a long way but more folks involved with SP in the UK should be at these events.

Mark Macrae, Anthony Pounder & Brett Lonsdale set up SharePoint Saturday, it's huge, the speakers are great and give their time.  People who attend are good to speak too.  So my thanks to the speakers, the 3 fellas mentioned above and Rik from BlackMarble (great conversation).

The presentations I saw were with my thoughts:
  • Wes Hackett (Bring SP into your Office with Apps for Office), I saw a fair amount of this at SPC but Wes brought some good ideas and presented the topic extremely well.  Apps are really powerful in Office and SP2013 but I'm not sold it's going to work as well as MS are preaching.
  • Bill Ayers (Lean-Agile Development with SharePoint), best session I went to.  Had a chat with Chris, Alan (Eardly) & Bill afterwards - really  good stuff, with a topic relevant to my current project.  Unit test and SP are not a great combination in my opinion sure you can using a Mocking framework but I'm not sold, the learning curve for the team is high (effective TDD takes time no matter how many converted devs tell me it's simple), you spend time mocking.  The list goes on and the right projects with the right management buy in 100% I'm behind it I just think it's probably less than 10% of SharePoint projects.   The main take away which I agree with is SCRUM for SP projects is great.  Implementing scrum has challenges but is worth the pain in most scenarios.  Testing is key, you can use Integration tests instead of Unit Tests in VS.  I advocate projects use Code UI testing as with most SP projects you get the most "bang for your buck" (not part of the talk).
  • Martin Hatch (Performance and Load Testing using Visual Studio), good overview with demo and walk thru.  It is amazing what VS 2012 ultimate.  You can definitely remove project risk by using these tools and it's no longer in the domain of load runner experts.  VS has firmly tooled dev and teams to monitor performance and determine bottlenecks.
  • Marjn Somers (Extending SP with Simple jQuery Solutions), this is a funny guy well at a SP conference the bar is pretty low in the comedy genre.  I felt it was too simple (and i'm a simpleton (not the pattern) - sic) but as a 101 into SP and jQuery excellent.
  • Adam Burcher (PowerShell - Let me script that for you!), was well presented.  It was an intro into PS and PS for SP. I didn't get much out of it but it was well presented and the demo of showing SP devs how easy it is to convert a C# event handler's code into PS was extremely effective.
Sessions I missed and would of like to see where Andrew Woodwards on Why you need a SharePoint Centre of Excellence (CoE), Paul Hunts session on Sift thru Search and deliver more, Chris O'Brien Getting to Grips with SP2013 Apps, Mark Macrae's BI talk would of been good to attend also.


 

Setting up VMs (VMware ESX5)


Overview:  VMware ESXi is a 1st class virtualisation platform and commonly used to host SharePoint VM's.  The post looks at the common tools for managing/setting up my VMware based infrastructure.  I'm certainly not an expert in virtualisation but this is a 101 in setting up VM's.
 
vSphere Client connects to the ESXi server infrastructure and provides a UI management tool.
 
PowerShell with PowerCLI to let you manage your ESX infrastructure using PowerShell. 
My preferred option is to use PowerGUI and user the add the PowerCli add in so I can interact with ESX but I have Intellisense and nice debugging capabilities.   The snippet below shows how to connect to the ESXi server.
Script expanded with more detail.  Really need to loop throu config to create multiple VM's and get their individual settings.
Before running the script ensure you have the OSCustomizationSpec and OS template as you need them to build.
Check the VM is created.
Summary:  Useful scripts for building a unique set of VM's on VMware.  For Continuous Integration it is better to start with pre-build environments.  My next step would be to fire off the SQL Server 2012 builds on 1 or more of these VM's as shown in the SQL Server 2012 slipstreamed install.

Thanks to Wayne Senior for info in this post.