【问题标题】:Powershell adds duplicate IIS Application PoolPowershell 添加重复的 IIS 应用程序池
【发布时间】:2019-07-07 07:29:35
【问题描述】:

我有以下 Powershell 脚本来在 IIS 中创建应用程序池和网站。该脚本在我第一次运行它时添加了应用程序池(当应用程序池不存在时)但是当我第二次运行该脚本时,虽然应用程序池存在,但脚本找不到它并尝试再次创建它.导致异常!

$WebsiteName="search-api"
$AppPoolName="search-api"
$Runtime=""   # Empty = Not Managed
$Port = 3050
$PhysicalPath="C:\Applications\SearchApi"

import-module WebAdministration

clear

New-Item -Path $PhysicalPath -Force

$AppPool = Get-IISAppPool -Name $AppPoolName

If ($AppPool.Length -eq 0)
{ 
    $AppPool = New-WebAppPool -Name $AppPoolName -Force 
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $Runtime
}


$TheWebSite = Get-Website -Name $WebsiteName

If ($TheWebSite -eq $null)
{
    New-Website -Name $WebsiteName -Port $Port -IPAddress "*" -ApplicationPool $AppPoolName -PhysicalPath  $PhysicalPath -Force 
}

【问题讨论】:

    标签: powershell iis


    【解决方案1】:

    第一个:避免PropertyNotFoundException

    If ( $AppPool.Length -eq 0 ) {  }
    

    在此对象上找不到属性“长度”。验证该属性是否存在。

    请改用If ( $null -eq $AppPool ) { }

    第二次:阅读AdminOfThingsOdd issue when using Get-IISAppPool and creating a new app pool in the same session的回答。

    要么申请Reset-IISServerManager(可能需要确认),

    或者,而不是Get-IISAppPool -Name $AppPoolName,使用

    Get-ChildItem IIS:\AppPools | Where-Object Name -eq $AppPoolName
    

    3rdNew-WebSite docs 说的是 -PhysicalPath 参数:

    指定新站点的物理路径。指定的文件夹 必须已经存在。

    使用New-Item -Path $PhysicalPath -Force -ItemType Directory

    【讨论】:

      【解决方案2】:

      您可以尝试以下脚本在 iis 中创建站点:

      Import-Module WebAdministration
      $iisAppPoolName = "my-test-app"
      $iisAppPoolDotNetVersion = "v4.0"
      $iisAppName = "mytest"
      $directoryPath = "C:\s2"
      
      #navigate to the app pools root
      cd IIS:\AppPools\
      
      #check if the app pool exists
      if (!(Test-Path $iisAppPoolName -pathType container))
      {
          #create the app pool
          $appPool = New-Item $iisAppPoolName
          $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
      }
      
      #navigate to the root of the site
      cd IIS:\Sites\
      
      #check if the site exists
      if (Test-Path $iisAppName -pathType container)
      {
          return
      }
      
      #create the site
      $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":87:"} -physicalPath $directoryPath
      $iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName
      

      【讨论】:

        猜你喜欢
        • 2010-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多