Windows Powershell Script to Create IIS Sites for Current User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IIS Site Creation Tool | |
# Verion: 0.0.1 | |
# Author: Tommy Ready <tommy.r.ready@gmail.com> | |
# Description: Powershell Tool for auto creation of IIS sites per the user logged in. | |
# Run Command: C:\> .\iis-site-creator.ps1 | |
$current_user = $env:UserName | |
$user_iis_folder = "C:\sites\$current_user" | |
$shared_directory = "C:\sites\shared" | |
$shared_media_direectory = $shared_directory + "\Data" | |
$sites =@( | |
[PSCustomObject]@{ | |
SiteName = "www1" | |
HostName = "www1.domain.com" | |
SharedDataDirectoryName = "www1_data" | |
GitRepo = "https://github.com/myrepo1.git" | |
}, | |
[PSCustomObject]@{ | |
SiteName = "www2" | |
HostName = "www2.domain.com" | |
SharedDataDirectoryName = "www2_data" | |
GitRepo = "https://github.com/myrepo2.git" | |
} | |
) | |
$iis_folder_acl_users =@( | |
[PSCustomObject]@{ | |
UserName = "IUSR" | |
Permission = "FullControl" | |
}, | |
[PSCustomObject]@{ | |
UserName = "IIS_IUSRS" | |
Permission = "FullControl" | |
} | |
) | |
foreach($site in $sites) { | |
$SiteFolder = Join-Path -Path $user_iis_folder -ChildPath $site.SiteName | |
$SiteName = $current_user + "_" + $site.SiteName | |
$HostName = $current_user + $site.HostName | |
# Make Sure Site Directory Exists | |
if(!(Test-Path -Path $SiteFolder )){ | |
New-Item -ItemType directory -Path $SiteFolder # If Not Create It | |
} | |
# Clone Repo | |
Write-Output "Cloning Repo +++++++++++++++++++++++++" | |
git clone $site.GitRepo $SiteFolder | |
# Create Symlink to Shared Config | |
Write-Output "Creating Symlink to Web.config +++++++" | |
New-Item -ItemType SymbolicLink -Path $SiteFolder -Name web.config -Value C:\sites\shared\web.config | |
if($site.SharedDataDirectoryName) { | |
# Copy Media/Templates/CSS | |
$MediaDirectory = $shared_media_direectory + "\" + $site.SharedDataDirectory | |
Write-Output "Copying Media to Site Folder +++++++++" | |
Copy-Item -Path $MediaDirectory -Destination $SiteFolder -recurse -Force | |
} | |
# Set User Permissions | |
Write-Output "Adding IIS Users ACL Rules ++++++++++" | |
foreach($aclUser in $iis_folder_acl_users) { | |
$CurrentAcl = Get-Acl $SiteFolder | |
$AclPermissions = $aclUser.UserName, $aclUser.Permission, "Containerinherit, ObjectInherit", "None", "Allow" | |
$AclRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $aclPermissions | |
$CurrentAcl.SetAccessRule($aclRule) | |
$CurrentAcl | Set-Acl -Path $SiteFolder | |
} | |
# Create IIS Site | |
Write-Output "Creating Site in IIS ++++++++++++++++" | |
New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force | |
$IISSite = "IIS:\Sites\$SiteName" | |
Set-ItemProperty $IISSite -name Bindings -value @{protocol="http";bindingInformation="*:80:$HostName"} | |
# Start the Site | |
Start-IISSite -Name $SiteName | |
Write-Output "Development Direcrory: $SiteFolder" | |
Write-Output "Development URL: $HostName" | |
Write-Output "===================================" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment