I have a csv file that contains 10 different website configurations.
I've created the script below to automate their creation. When I test the script, it runs succesfully, but I'm wondering if this is the best design to automate website creation. Is my use of the loop and the if/else statements correct?
I'm hoping someone can review and let me know if this script looks good.
I'd also like to know if there is a way to set different permissions on different virtual directories? For example, if I had a folder that is used for uploads and downloads, is there a way, through a powershell script, to set read/write permissions for that virtual directory?
Thanks!
#populate array object with contents from webservers.csv file
$webObjects = Import-Csv c:\\iis\webservers.csv
ForEach ($obj in $webObjects) {
$name = $obj.WebSiteName
$path = $obj.Path
$vPath = $obj.VirtualDirectory
$appPool = $obj.AppPoolName
$appPoolId = $obj.appPoolIdentity
$dotNetVersion = $obj.DotNetVersion
$port = $obj.Port
#new website path
if ( ! (Test-Path $path)) {
New-Item -type directory -path $path
} else {
Write-Host "The path already exists." -BackgroundColor Blue -ForegroundColor White
}
#new website virtual path
if ( ! (Test-Path $vPath)) {
New-Item -type directory -path $vPath
} else {
Write-Host "The path already exists." -BackgroundColor Blue -ForegroundColor White
}
#New application pool
if ( ! (Test-Path "iis:\appPools\$appPool")) {
New-WebAppPool $appPool
} else {
Write-Host "This application pool already exists." -BackgroundColor Blue -ForegroundColor White
}
if (Test-Path "iis:\appPools\$appPool") {
Set-ItemProperty IIS:\AppPools\$appPool managedRuntimeVersion $dotNetVersion
} else {
Write-Host "This application pool does not exist." -BackgroundColor Blue -ForegroundColor White
}
#New website
if ( ! (Test-Path "iis:\Sites\$name")) {
New-WebSite -Name $name -PhysicalPath $path -ApplicationPool $appPool -Port $port
} else {
Write-Host "A website with the name $name at $path already exists." -BackgroundColor Blue -ForegroundColor White
}
#New virtual directory
if ( ! (Test-Path $vPath)) {
New-WebVirtualDirectory -Site $name -Name $name -PhysicalPath -$vPath
} else {
Write-Host "A virtual directory with the name $name at $vPath already exists." -BackgroundColor Blue -ForegroundColor White
}
}