在 ServiceDefinition.csdef 中:
<Task commandLine="makevdir.cmd" executionContext="elevated" taskType="background" />
Makevdir.cmd:
PowerShell -ExecutionPolicy Unrestricted .\makevdir.ps1 >> "%RoleRoot%\makevdir.txt" 2>&1
EXIT /B %errorlevel%
Makevdir.ps1:
# Make Virtual Directory for ImageResizer DiskCache plugin (/imagecache)
Write-Host "makevdir.ps1 starting"
# $ImageCacheDir = $env:temp + "\imagecache"
#
# The TEMP paths are quota limited to ~100MB, so use a hard coded path:
$ImageCacheDir = "C:\Resources\AspNetTemp\imagecache"
Write-Host "ImageCacheDir = $ImageCacheDir"
md -Path $ImageCacheDir
# Set the ACL to allow IIS access to the new directory
$acl = Get-Acl $ImageCacheDir
$identity = "IIS_IUSRS"
$fileSystemRights = "Modify"
$inheritanceFlags = "ContainerInherit, ObjectInherit"
$propagationFlags = "None"
$accessControlType = "Allow"
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($identity, $fileSystemRights, $inheritanceFlags, $propagationFlags, $accessControlType)
$acl.SetAccessRule($rule)
Set-Acl $ImageCacheDir $acl
# Get the AppName by appending _Web/ to the RoleInstanceID environmental variable
$AppName = $env:RoleInstanceID + "_Web/"
Write-Host "AppName = $AppName"
# Retry creating the virtual directory every five seconds for a maximum of 20 tries
$tries = 0
& $Env:WinDir\system32\inetsrv\appcmd.exe add vdir /app.name:$AppName /path:/imagecache /physicalpath:$ImageCacheDir
while (!$? -and $tries -lt 20) {
$tries++
sleep 5
& $Env:WinDir\system32\inetsrv\appcmd.exe add vdir /app.name:$AppName /path:/imagecache /physicalpath:$ImageCacheDir
}
Write-Host "makevdir.ps1 exiting"
Exit