Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Script to set up ASP.NET development environment in IIS with SQL Express using Network Service
# Originally from http://poshcode.org/3819
function Add-ToHostsFile {
<#
.DESCRIPTION
This function checks to see if an entry exists in the hosts file.
If it does not, it attempts to add it and verifies the entry.
.EXAMPLE
Add-ToHostsFile -IPAddress 192.168.0.1 -HostName MyMachine
.EXTERNALHELP
None.
.FORWARDHELPTARGETNAME
None.
.INPUTS
System.String.
.LINK
None.
.NOTES
None.
.OUTPUTS
System.String.
.PARAMETER IPAddress
A string representing an IP address.
.PARAMETER HostName
A string representing a host name.
.SYNOPSIS
Add entries to the hosts file.
#>
param(
[parameter(Mandatory=$true,position=0)]
[string]
$IPAddress,
[parameter(Mandatory=$true,position=1)]
[string]
$HostName
)
$HostsLocation = "$env:windir\System32\drivers\etc\hosts";
$NewHostEntry = "$IPAddress`t$HostName";
if ((Get-Content $HostsLocation) -contains $NewHostEntry) {
Write-Host "The hosts file already contains the entry: $NewHostEntry. File not updated.";
} else {
Write-Host "The hosts file does not contain the entry: $NewHostEntry. Attempting to update.";
Add-Content -Path $HostsLocation -Value $NewHostEntry;
}
# Validate entry
if ((Get-Content $HostsLocation) -contains $NewHostEntry) {
Write-Host "New entry, $NewHostEntry, added to $HostsLocation.";
} else {
Write-Host "The new entry, $NewHostEntry, was not added to $HostsLocation.";
}
}
# Localhost host file entries - alternatively see: http://readme.localtest.me
. ".\Add-ToHostsFile.ps1"
Add-ToHostsFile -IpAddress 127.0.0.1 -HostName {LOCAL_APP_DOMAIN_NAME}
# Setup site and tests databases
& sqlcmd -S ".\SQLEXPRESS" -E -i "SetupDevelopmentEnvironment.sql"
# Set up IIS site / app pool
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$physicalPath = Join-Path $directorypath "{DIRECTORY_OF_WEB_PROJECT}"
& c:\Windows\system32\inetsrv\AppCmd.exe add apppool /name:{APP_POOL_NAME} /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated
& c:\Windows\system32\inetsrv\AppCmd.exe set config /section:applicationPools "/[name='{APP_POOL_NAME}'].processModel.identityType:NetworkService"
& c:\Windows\system32\inetsrv\AppCmd.exe add site /name:"{SITE_NAME}" /physicalPath:$physicalPath /bindings:http/*:80:{LOCAL_APP_DOMAIN_NAME}
# If you want to add HTTPS (but you need an appropriate SSL cert installed)
#& c:\Windows\system32\inetsrv\AppCmd.exe set site /site.name {SITE_NAME} "/+bindings.[protocol='https',bindingInformation='*:443:{LOCAL_APP_DOMAIN_NAME}']"
& c:\Windows\system32\inetsrv\AppCmd.exe set app "{SITE_NAME}/" /applicationPool:"{APP_POOL_NAME}"
# Change anonymous identity to auth as app-pool identity instead of IUSR_...
& c:\Windows\system32\inetsrv\AppCmd.exe set config /section:anonymousAuthentication /username:"" --password
# Give Network Service persmission to read the site files
& icacls "$physicalPath" /inheritance:e /T /grant """NETWORK SERVICE:(OI)(CI)F"""
USE master
GO
IF db_id('{DATABASE_NAME}') IS NOT NULL BEGIN
PRINT 'Dropping database...'
ALTER DATABASE {DATABASE_NAME}
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
DROP DATABASE {DATABASE_NAME};
PRINT 'Dropped database.'
END ELSE BEGIN
PRINT 'Database does not exist. Not dropping it :)'
END
PRINT 'Creating database...'
CREATE DATABASE {DATABASE_NAME}
PRINT 'Created database.'
USE {DATABASE_NAME};
GO
exec sp_addrolemember 'db_owner', 'NT AUTHORITY\NETWORK SERVICE';
GO

Royce commented Oct 17, 2014

On my computer, the .sql script fails at line 26... Is it missing a "GO" after line 23?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment