從 csv 檔案和資料夾權限建立網站

從 csv 檔案和資料夾權限建立網站

我有一個 csv 文件,其中包含 10 種不同的網站配置。

我創建了下面的腳本來自動建立它們。當我測試腳本時,它成功運行,但我想知道這是否是自動化網站創建的最佳設計。我對迴圈和 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
}

}

相關內容