theme-sticky-logo-alt

Create a scripted SharePoint 2013 Development Environment Tutorial – Part 6

5 Comments

Introduction

Creating a SharePoint Development Virtual Machine is something you might have to do quite a few times as a Developer. Even if it’s a pretty easy process, on a slow machine it can easily kill 4-5 hours of your day only to install SQL, SharePoint, Visual Studio and all the others tools you might need to get started. What if you could just script it so it’s all done automatically while you go take coffee, chat on SharePoint Community or whatever else you enjoy doing? In this tutorial we will learn how to build a script that we will keep using for as long as you keep developing on SharePoint 2013. The tutorial will be split in a few sections since in order to keep them focused and short, and at the end they will all be published in a downloadable PDF. I strongly encourage you to not only copy the scripts, but actually read the blog posts of why I did them like this and there are some parts where you will need to work as well!

Sections:

  1. Introduction & Prerequisites & Creating the Domain and Service Accounts
  2. Installing software prerequisites + SQL
  3. Installing SharePoint 2013
  4. Installing Visual Studio 2012 and Optional Software
  5. Basic Optimizing and Summary
  6. Advanced Optimizing of the scripts! (You are here)
  7. An eBook Guide of the 6 sections and a video! 

Advanced Optimization

Welcome to the advanced optimization part of the script. With this section we will make our script go from 6 scripts, to only running one script and then not touching it until it’s done! I warn you that this section will go a lot “Faster” trough the steps because I assume after the first 5 tutorials you got a better understanding of PowerShell! First of all, I want you to create a “Launchers” Folder and a config.xml file in the C:\SharePoint folder. We will see what we put in them later!

Open the config.xml file and we will add the following things. The localadminpassword is the password of your “local administrator” (the account you use before creating the domain), and the serviceaccountpwd is the password you will use for your Service Accounts!

<autovm>
<localadminpassword>pass@word1</localadminpassword>
<serviceaccountpwd>pass@word1</serviceaccountpwd>
</autovm>

Now, open the preparepc.ps1 script and add the following after the “Add-WindowsFeature -Name “ad-domain-services” -IncludeAllSubFeature –IncludeManagementTools” line.

$xmlconfig = "C:\SharePoint\config.xml"
$propertiesInput = Get-Content $xmlconfig
$password = $propertiesInput.autovm.localadminpassword
New-Item -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\" -Name RunOnce -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name SPVM -Value "`"C:\SharePoint\Launchers\createdomain.bat`" " -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value "Administrator"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $password
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value "1"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value "1"
Restart-Computer

Let’s see what we did there! First, we took the password from the config.xml file and saved it in a variable called $password. Afterwards, since we have a reboot we will need a way to start the scripts… so we will use the “RunOnce” registry key. The RunOnce key is what the PC will do the next time a user logs on… in this case we will run “C:\SharePoint\Launchers\createdomain.bat” which we haven’t yet created! Now, the “Winlogon” registry values are there for the autologin. We know our initial account is called “Administrator” and using the $password variable, Windows will be able to automatically log us in!

Now go in the Launchers Folder, and create a file called createdomain.bat that has the following inside:

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit -ExecutionPolicy Bypass C:\SharePoint\2-createdomain.ps1'"

Microsoft’s PowerShell Script scheduling isn’t 100% there yet, and I prefer to use .bat files to start our programs. This .bat will start PowerShell as an administrator and run the createdomain.ps1 script!

Now, let’s open the createdomain.ps1 script and at the beginning of the script add the following:

# Windows PowerShell script for AD DS Deployment
$PlainPassword = "pass@word1"
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$xmlconfig = "C:\SharePoint\config.xml"
$propertiesInput = Get-Content $xmlconfig
$password = $propertiesInput.autovm.localadminpassword
New-Item -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\" -Name RunOnce -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name SPVM -Value "`"C:\SharePoint\Launchers\createusers.bat`" " -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value "Administrator"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $password
New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\WinLogon" -Name DefaultDomainName -Value "VLADDEV"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value "1"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value "1"

The $PlainPassword and $SecurePassword are the same string which will be used for the SafeModeAdministrator password, so it doesn’t ask us for the password anymore. The rest is a bit the same except since we will now be in a domain, we added a new-itemproperty (DefaultDomainName).. makesure you change VLADDEV to your actual domain. While in the same script, we will add this line “-SafeModeAdministratorPassword:$SecurePassword ` ” somewhere in the Install-ADDSForest command. It will look something like this:

SharePoint 2013 Development Environment Tutorial

Now we go in Launchers and create the “createusers.bat” file which will contain the following:

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit -ExecutionPolicy Bypass C:\SharePoint\3-createaccounts.ps1'"

We are going to edit the createaccounts.ps1 file to look something like this:

#Name: createaccounts.ps1
#Purpose: Creates Service Accounts
cd c:\SharePoint\sp\serviceaccounts
.\sp2013serviceaccounts.ps1 -Level high -SPOU "SP Service Accounts" -SQLOU "SQL Service Accounts" -SQLLevel high -OptionalAccounts $true
cd c:\SharePoint
.\4.prereqsandfeatures.ps1

The Reason is that the account script and prereqs script can be done with the same users without reboot, so might as well queue them. Now open the sp2013serviceaccounts.ps1 file and find the line where you have $defaultpassword = Read-host …. . We are going to replace it with the password we get from our config file. So it should look something like this:

# get default password. If user put new password in XML, the XML will have priority
$xmlconfig = "C:\SharePoint\config.xml"
$propertiesInput = Get-Content $xmlconfig
$defaultpassword = $propertiesInput.autovm.serviceaccountpwd

Now Open the prereqsandfeatures.ps1 file and before the Restart-Computer command, add the following:

New-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\" -Name RunOnce -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name SPVM -Value "`"C:\SharePoint\Launchers\installsql.bat`" " -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value "sql_admin"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $password
New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\WinLogon" -Name DefaultDomainName -Value $env:USERDOMAIN -PropertyType "String" -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value "1"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value "1"

Now, in the Launchers folder, create the installsql.bat file with this inside:

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit -ExecutionPolicy Bypass C:\SharePoint\5.installsql.ps1'"

Good so now we got everything up until SQL to do itself. Now, open the installsql.ps1 script and after the .\SQLBinaries…. Line add the following

$xmlconfig = "C:\SharePoint\config.xml"
$propertiesInput = Get-Content $xmlconfig
$password = $propertiesInput.autovm.serviceaccountpwd
New-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\" -Name RunOnce -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name SPVM -Value "`"C:\SharePoint\Launchers\installSharePoint.bat`" " -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value "sp_admin"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value $password
New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\WinLogon" -Name DefaultDomainName -Value $env:USERDOMAIN -PropertyType "String" -Force | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value "1"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value "1"
shutdown -l

Now, in the Launchers folder, create the installSharePoint.bat file with this inside:

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit -ExecutionPolicy Bypass C:\SharePoint\6.installSharePoint.ps1'"

Now we’re OK up to the installation of SharePoint. Open the script AutoSPInstallerMain.ps1 from C:\SharePoint\SP\AutoSPInstaller\ and go at the very end. (Try to do a search for ” If ($isTracing) {Stop-Transcript; $script:isTracing = $false}” ) . After that line, add the following:

cd c:\sharepoint
.\7.installvs2012andtools.ps1

Now… we do not have that PowerShell file yet however since both VisualStudio2012 and the Tools and Software are done with the same user, we will combine script 7 & 8 in one script that will be called: 7.installvs2012andtools.ps1 . We will also modify how we see if Visual Studio 2012 is installed. In theory, there is a switch called /forcereboot that would reboot when Visual Studio is done installing… However, it doesn’t workL. So, I had the creative and ghetto idea to do this: Log the installation and when there is no writing in the logs for 5 minutes, it means that the installation is finished! So here is how the Visual Studio 2012 installation is now done:

Write-Host "Installing Visual Studio 2012"
New-Item -ItemType directory -Path C:\AutoInstallLogs
cd C:\SharePoint\Software\VisualStudio2012
.\vs_ultimate.exe /full /noweb /quiet /ProductKey XXXXXXXYYYYZZZZJJDJDJ /Log C:\AutoInstallLogs\VS2012.txt
Start-Sleep -Seconds 240
$path = "C:\AutoInstallLogs"
$y = gci $path |where{!($_.psiscontainer)}|sort -desc lastwritetime|select -first 1
$a = Get-Date
$timespan = New-TimeSpan -Start $y.lastwritetime -End $a
while ($timespan.Minutes -lt 5)
{
Start-Sleep -Seconds 120
$y = gci $path |where{!($_.psiscontainer)}|sort -desc lastwritetime|select -first 1
$a = Get-Date
$timespan = New-TimeSpan -Start $y.lastwritetime -End $a
}
Write-Host "Visual Studio 2012 Installation Done"

It’s probably not the best or cleanest way… however it works perfectly! Here is the rest of the script:

Write-Host "Installing Microsoft Office Developer Tools for Visual Studio 2012"
C:\SharePoint\Software\OfficeToolsForVS2012RTW\WebPlatformInstaller_amd64_en-US.msi /q
Start-Sleep -s 120
C:\SharePoint\Software\OfficeToolsForVS2012RTW\bin\WebpiCmd.exe /install /products:OfficeToolsForVS2012RTW /XML:C:\SharePoint\Software\OfficeToolsForVS2012RTW\feeds\latest\webproductlist.xml /AcceptEula
Write-Host "Installing NotePad++"
cd C:\SharePoint\Software\NotePad++
.\npp.6.5.Installer.exe /S
Start-Sleep -s 120
Write-Host "Installing Goolge Chrome"
Cd C:\SharePoint\Software\Chrome
Msiexec /q /I GoogleChromeStandaloneEnterprise.msi
Start-Sleep -s 120
Write-Host "Installing FireFox"
cd C:\SharePoint\Software\FireFox
.\ffsetup.exe -ms
Start-Sleep -s 120
Write-Host "Installing SharePoint Designer 2013"
cd C:\SharePoint\Software\SPDesigner
.\setup.exe /adminfile updates\adminfile.msp
Start-Sleep -s 200
Write-Host "Copying standalone programs"
Copy-Item C:\SharePoint\Software\StandAlone\* C:\Users\sp_admin\Desktop
Write-Host "DONE"

I know there weren’t many images in this part as in the other one, so here are some to give you an idea what it should look like at the end:

Hope you enjoyed and head over to Part 7 (19-11-2013) which is the final Summary and a 1h30 long video of how your installation will look like!

Share this post with your followers on twitter:

Leave  a comment and don’t forget to like us on Facebook here and to follow me on Google+ here and on Twitter here  for the latest news and technical articles on SharePoint.  Also, don’t forget to check out SharePoint Community.Net for more great SharePoint Content

Previous Post
This link no longer works, because it may have been moved or deleted. You can try to find it and follow it in its new location.
Next Post
The Ultimate Guide to SharePoint Server 2013 Certifications V2

5 Comments

Leave a Reply

15 49.0138 8.38624 1 0 4000 1 https://vladtalkstech.com 300 1