使用Powershell在非应用程序的文件夹中创建Web应用程序

问题描述:

我想在IIS中创建一个不在IIS站点根目录的Web应用程序。

I want to create a web application in IIS that does not live at the root of the IIS site.

即。 MySite / beta / WebApplication。

i.e. MySite/beta/WebApplication.

这是我的出发点:


新-WebApplicationWebApplication-SiteMySite-ApplicationPoolMyAppPool-PhysicalPathC:\ Sites \ MySite \beta \WebApplication

New-WebApplication "WebApplication" -Site "MySite" -ApplicationPool "MyAppPool" -PhysicalPath "C:\Sites\MySite\beta\WebApplication"

这创建了我想要的物理结构 C:\Sites\MySite \beta \WebApplication ,但是让IIS看起来像这样: / p>

That creates me the physical structure I want C:\Sites\MySite\beta\WebApplication , but makes IIS look like this:


MySite(IIS网站)

MySite (IIS Web Site)


WebApplication (IIS WebApplication)

WebApplication (IIS WebApplication)

beta(文件夹)


WebApplication(文件夹)

WebApplication (Folder)



有没有办法可以通过powershell完成?我真的不希望 beta 成为一个Web应用程序,只是一个文件夹。

Is there a way this can be done via powershell? I do not really want beta to be a web application, just a folder.

我知道这篇文章有点旧了但这里是我编写的powershell脚本,它将现有文件夹转换为IIS中的Web应用程序,或者如果它不存在则创建一个新的文件夹和Web应用程序。它还为它创建了应用程序池。它接收一组应用程序名称,以便您可以创建多个Web应用程序。这是我的第一个powershell脚本,所以如果你有任何建议可以随意发表评论。

I know this post is a little older but here is a powershell script I wrote that converts an existing folder to a web application in IIS or if it doesn't exist creates a new folder and web app. It also creates the app pool for it as well. It receives an array of app names so you can create more than one web application. This was my first powershell script so if you have any suggestions feel free to comment.

#Receives an array of appnames and creates the app pools and web applications or converts the folder to an application

Param([parameter(Mandatory=$true)][string[]]$appNames)
$useDefaultPhysicalPath = Read-Host "Would you like to use the default physical path? (C:\inetpub\wwwroot\)";
Import-Module WebAdministration;

$physicalPath = "C:\inetpub\wwwroot\";
if(!($useDefaultPhysicalPath.ToString().ToLower() -eq "yes" -or $useDefaultPhysicalPath.ToString().ToLower() -eq "y"))
{
   $physicalPath = Read-Host "Please enter the physical path you would like to use with a trailing \ (do not include the app name)";
}


$appPath = "IIS:\Sites\Default Web Site\";

foreach($appName in $appNames)
{

if((Test-Path IIS:\AppPools\$appName) -eq 0)
{

    New-WebAppPool -Name $appName -Force;
}

if((Test-Path $appPath$appName) -eq 0 -and (Get-WebApplication -Name $appName) -eq $null)
{  
    New-Item -ItemType directory -Path $physicalPath$appName; 
    New-WebApplication -Name $appName -ApplicationPool $appName -Site "Default Web Site" -PhysicalPath $physicalPath$appName;
}
elseif((Get-WebApplication -Name $appName) -eq $null -and (Test-Path $appPath$appName) -eq $true)
{
    ConvertTo-WebApplication -ApplicationPool $appName $appPath$appName;
}
else
{
    echo "$appName already exists";
}
}