Udostępnij za pośrednictwem


PowerShell - Configure Web Application use IIS in a NuGet Package

Configure Web Application use IIS in a NuGet Package

As part of an internal NuGet package I want to be able to change a Web Application to run using the IIS instead of the IIS Express. This can be done by using the PowerShell script below.

It uses a EnvDTE Project for setting a number of properties on the project, but to create the virtual directory to make it work seamslessly with IIS I use appcmd.exe.

As text: 

 param($installPath, $toolsPath, $package, $project)
 
 $appName = $project.Name.split(".")[0]
 $strip = $project.Name + ".csproj"
 $physicalPath = $project.FullName.TrimEnd($strip)
 
 Write-Host "Detected Web Application name as: $appName"
 
 Write-Host "Detected Web Application physical path as: $physicalPath"
 
 Write-Host "Setting IIS Properties of the current Web Application project"
 foreach ($prop in $project.Properties)
 {
 if($prop.Name -eq "WebApplication.UseIIS")
 {
 Write-Host 'Updating project setting' $prop.Name 'to: '$true 'was: '$prop.Value
 $prop.Value = $true
 }
 elseif($prop.Name -eq "WebApplication.IsUsingIISExpress")
 {
 Write-Host 'Updating project setting' $prop.Name 'to: '$true 'was: '$prop.Value
 $prop.Value = $false
 }
 elseif($prop.Name -eq "WebApplication.UseIISExpress")
 {
 Write-Host 'Updating project setting' $prop.Name 'to: '$true 'was: '$prop.Value
 $prop.Value = $false
 }
 elseif($prop.Name -eq "WebApplication.IISUrl")
 {
 Write-Host 'Updating project setting' $prop.Name 'to: '$true 'was: '$prop.Value
 $prop.Value = "https://localhost/$appName"
 } 
 }
 
 $project.Save();
 
 Write-Host "Setting physical path of the Web Application in IIS under Default Web Site"
 
 $defaultWebSiteName = "Default Web Site"
 $appCmdDirectory = "$env:SystemRootSystem32inetsrv"
 $IISAppPath = $defaultWebSiteName + "/" + $appName
 
 $cmd = "$appCmdDirectory/appcmd.exe set app /app.name ""$IISAppPath"" /[path='/'].physicalPath:$physicalPath"
 cmd /C $cmd