Using Powershell to create a web application in a folder that is not an application

前端 未结 3 1215
情深已故
情深已故 2021-02-09 16:30

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

i.e. MySite/beta/WebApplication.

This is my starting point:

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-09 17:06

    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";
    }
    }
    

提交回复
热议问题