-
Notifications
You must be signed in to change notification settings - Fork 14
/
CreateVirtualDirectory.ps1
51 lines (40 loc) · 2.29 KB
/
CreateVirtualDirectory.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## Intro: Simple powershell script to install (or replace) a virtual directory
## Usage: CreateVirtualDirectory.ps1 [WebsiteName] [FolderName] [PhysicalPath] ([domain\user] [password])
## Note : These scripts require local admin priviliges!
# Load IIS tools
Import-Module WebAdministration
sleep 2 #see http://stackoverflow.com/questions/14862854/powershell-command-get-childitem-iis-sites-causes-an-error
# Get SiteName and AppPool from script args
$siteName = $args[0] # "default web site"
$folderName = $args[1] # "Files"
$physicalPath = $args[2] # "\\UncServer\UncShare"
$user = $args[3] # "domain\username"
$password = $args[4] # "password1"
if($siteName -eq $null) { throw "Empty site name, Argument one is missing" }
if($folderName -eq $null) { throw "Empty folder name, Argument two is missing" }
if($physicalPath -eq $null) { throw "Empty PhysicalPath, Argument three is missing" }
$backupName = "$(Get-date -format "yyyyMMdd-HHmmss")-$siteName"
"Backing up IIS config to backup named $backupName"
$backup = Backup-WebConfiguration $backupName
try {
"Adding a virtual directory '$folderName' to website '$siteName' for physical path $physicalPath"
## Init
$virtualDirectoryPath = "IIS:\Sites\$siteName\$folderName"
## Create Virtual Directory where physicalpath is an UNC-path (New-WebVirtualDirectory wont do)
$newItem = New-Item $virtualDirectoryPath -type VirtualDirectory -physicalPath $physicalPath -Force
if($user -ne $null) {
"Setting virtual directory Connect-As credentials to user $user"
## Change 'Connect As' settings (New-WebVirtualDirectory don't include Username and Password)
Set-ItemProperty $virtualDirectoryPath -Name userName -Value $user
Set-ItemProperty $virtualDirectoryPath -Name password -Value $password
}
#"Virtual directory settings:"
#Get-Item -Path $virtualDirectoryPath | fl *
"Virtual directory '$folderName' created sucessfully"
} catch {
"Error detected, running command 'Restore-WebConfiguration $backupName' to restore the web server to its initial state. Please wait..."
sleep 3 #allow backup to unlock files
Restore-WebConfiguration $backupName
"IIS Restore complete. Throwing original error."
throw
}