Monday 18 March 2013

PowerShell SharePoint web site Warm-up Script

Overview: Useful PowerShell Script to check when a SharePoint website is up and running, I use this a a trigger in my build process.  It can also be used as a warm up script for demo's.

Download SharePoint Warmup script.
Another Nice SP Warmup script for SharePoint.

For public websites you can also use a monitoring service to ensure your website is constantly warm such as AlertFox.

Another Script:
http://www.justinkobel.com/post/2013/08/16/My-SharePoint-2013-(and-2010)-Warm-up-Script.aspx

Tuesday 5 March 2013

Create Web Service Installer using MSI

Overview: The post show how to create an MSI or exe that can be used to deploy assets onto servers.  In this post it will look at packaging up a web service that is added to the msi installer and a custom project is created that will build the IIS instance to host the web service.






http://www.codeproject.com/Articles/16767/How-to-Pass-Command-Line-Arguments-to-MSI-Installe

Thursday 21 February 2013

PS3 with ISE

Key new features:
  • Intellesense
  • Autocomplete with the tab key
  • Regions - not only using the regions keyword but you can also fold up statements for easier reading of code.  A good example is if statments can be foldeed down.
  • Snippets - Can add you own custom snipptes, to see the ISE custom snippets when writing PS click "ctrl + j".  Easy to add you woen snippets.
  • "Show Command2 tool option in ISE is powerful for building PS.
Note: Don't use PowerShell 3 with AutoSPInstaller 3.2 for SP2010 (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.

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