从 csv 文件和文件夹权限创建网站

从 csv 文件和文件夹权限创建网站

我有一个包含 10 种不同网站配置的 csv 文件。

我创建了下面的脚本来自动创建它们。当我测试脚本时,它成功运行,但我想知道这是否是自动创建网站的最佳设计。我对循环和 if/else 语句的使用是否正确?

我希望有人可以审阅并告诉我这个脚本是否不错。

我还想知道是否有办法在不同的虚拟目录上设置不同的权限?例如,如果我有一个用于上传和下载的文件夹,是否有办法通过 powershell 脚本为该虚拟目录设置读/写权限?

谢谢!

#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
}

}

相关内容