在本文中您将学会如何去创建WEB站点,Web Applications, 虚拟目录和应用程序池.

介绍

      IIS PowerShell 名空间包括诸如: Web-Sites, Apps, Virtual Directories 和 Application Pools.  使用内置的PowerShell cmdlets可以很容易创建一个名空间项和管理该项.

创建Web站点

      如果您熟悉PowerShell 的话,就会知道在各种PowerShell 名空间下New-Item cmdlet 通常被用于创建 新项。 举个例子,当前命令 "New-Item c:\TestDirectory" 会创建一个新的文件夹 (尽管多数人使用New-Item
的别名命令 "MD" 或 " MKDIR" ). 在IIS 7.0 PowerShell 名空间下,New-Item 也常用于创建新的Web站点.

参数

      在创建一个文件系统路径时,您需要指定一个路径名称. 不巧的是当前创建WEB站点时这是不够的. 除了像 文件系统路径这样的参数之外,还需要network bindings. 下面的命令用于创建一个新的WEB站点并使用dir命令 进行显示:
 
PS IIS:\Sites >  New - Item iis:\Sites\TestSite  - bindings  @ {protocol = " http " ;bindingInformation = " :80:TestSite " - physicalPath c:\test

PS IIS:\Sites
>  dir

Name             ID   State      Physical Path                  Bindings
----               --     -----        -------------                    --------
Default  Web Site  1     Started    f:\inetpub\wwwroot             http  * : 80 :
TestSite         
2     Started    c:\test                        http : 80 :TestSite 

 
     这里直接使用了 -physicalPath 参数. 然而您可能会问: -bindings 看起来咋这么复杂?.
     在构造时通常使用hashtable (在 这里 了解更多PowerShell hash tables信息). hash table 中的键值对表示一个 设置集合,该集合在IIS站点bindings section中会反射出相关属性:
 
< bindings >
        
< binding  protocol ="http"  bindingInformation =":80:TestSite"   />
</ bindings >  
 
      现在我们找出了一个使用hash table的原因: IIS 配置是可以使用属性进行扩展的。 (查看 这里  了解更多信息) . 您 可以想像一下使用其它属性扩展 <binding> 元素节点.  hash table 的键值对提供了这种弹性.
      坦白说,该语法有一点复杂. 我们正在考虑在Tech Preview中封装一些典型任务:比如创建站点的方法或脚本。

删除站点

      下面是删除刚创建的站点.

PS IIS:\  > Remove - Item IIS:\Sites\TestSite 

创建Web Applications

    创建Web Applications 要比创建站点要容易. 下面: 
PS IIS:\ >  New - Item 'IIS:\Sites\ Default  Web Site\DemoApp'  - physicalPath c:\test  - type Application 
 

Name                     ApplicationPool          EnabledProtocols         PhysicalPath
----                       ---------------            ----------------           ------------
DemoApp                  DefaultAppPool           http                     c:\test 
 
     您仅需指定的一个参数是 type (-type) ,因为在一个站点下,您可能要创建一个Applications 或一个虚拟路径.
通过指定 -type 参数,就会告之 IIS Provider 要创建一个application.
 
     要删除这个 application 的话,也可以使用Remove-Item(见上面删除站点).  

创建虚拟目录

      要创建虚拟目录,您也要使用New-Item cmdlet. 下面会在指定站点(Default Web Site)下创建两个虚拟目录。
一个位于站点下,一个位于刚创建的 Web Application (DemoApp)下面.
 
PS IIS:\ >  New - Item 'IIS:\Sites\ Default  Web Site\DemoVirtualDir1'  - type VirtualDirectory  - physicalPath c:\test\virtualDirectory1

Name                                              PhysicalPath
----                                                ------------
DemoVirtualDir1                                   c:\test\virtualDirectory1


PS IIS:\
>  New - Item 'IIS:\Sites\ Default  Web Site\DemoApp\DemoVirtualDir2'  - type VirtualDirectory  - physicalPath c:\test\virtualDirectory2

Name                                              PhysicalPath
----                                                ------------
DemoVirtualDir2                                   c:\test\virtualDirectory2

 
创建应用程序池
 
      创建应用程序池更简单. 仅需指定一个名称即可.
PS IIS:\ >  new - item AppPools\DemoAppPool

Name                     State
----                       -----
DemoAppPool              {}
      简单吧,不是吗? 现在让我们将这些知识点连起来做一个完整的方案.

将所有知识点连在一起

    在这个方案中,我们将执行下面这些步骤:
  1. 创建一系列的文件系统路径用于绑定站点, web applications 和虚拟文件。
  2. 向新创建的目录下拷贝一些简单的web内容。
  3. 创建一个新的应用程序池。
  4. 创建一个新的站点,一个新的 application 以及两个虚拟目录并将其绑到新创建的应用程序池上。
  5. 使用浏览器请求(访问这些内容)。

步骤 1: 创建目录

   我们使用 New-Item cmdlet 来创建新的文件系统目录. 执行下面命令 (如不想指定-type 参数的话,可使用'md'):
New - Item C:\DemoSite  - type Directory

New
- Item C:\DemoSite\DemoApp  - type Directory

New
- Item C:\DemoSite\DemoVirtualDir1  - type Directory

New
- Item C:\DemoSite\DemoVirtualDir2  - type Directory 
 

步骤 2: 拷贝内容

      现在向这些目录中写入一些 html 内容(文件):
Set - Content C:\DemoSite\ Default .htm  " DemoSite Default Page "

Set
- Content C:\DemoSite\DemoApp\ Default .htm  " DemoSite\DemoApp Default Page "

Set
- Content C:\DemoSite\DemoVirtualDir1\ Default .htm  " DemoSite\DemoVirtualDir1 Default Page "

Set
- Content C:\DemoSite\DemoVirtualDir2\ Default .htm  " DemoSite\DemoApp\DemoVirtualDir2 Default Page "
 
 

步骤 3: 创建新的应用程序池

     为站点创建一个新的应用程序池 'DemoAppPool' (如之前的例子中您也创建的话,请先删除它).  
New - Item IIS:\AppPools\DemoAppPool

Step 4: 创建新的站点, Web Applications 和虚拟目录并绑定到应用程序池上

     这里简化一下,我们创建 DemoSite, DemoApp 和两个虚拟目录:
      DemoVirtualDir1 绑定到DemoSite 下
      DemoVirtualDir2 绑定到DemoApp 下
     我们将DemoSite 和 DemoApp 绑定到之前创建的 DemoAppPool 上. 为了不与'Default Web Site' 冲突,
这里DemoSite 的端口设置为 8080: 
New - Item IIS:\Sites\DemoSite  - physicalPath C:\DemoSite  - bindings  @ {protocol = " http " ;bindingInformation = " :8080: " }

Set
- ItemProperty IIS:\Sites\DemoSite  - name applicationPool  - value DemoAppPool

New
- Item IIS:\Sites\DemoSite\DemoApp  - physicalPath C:\DemoSite\DemoApp  - type Application

Set
- ItemProperty IIS:\sites\DemoSite\DemoApp  - name applicationPool  - value DemoAppPool

New
- Item IIS:\Sites\DemoSite\DemoVirtualDir1  - physicalPath C:\DemoSite\DemoVirtualDir1  - type VirtualDirectory

New
- Item IIS:\Sites\DemoSite\DemoApp\DemoVirtualDir2  - physicalPath C:\DemoSite\DemoVirtualDir2  - type VirtualDirectory

 
     好,下面就要请求这些web 内容了。

步骤 5: 请求Web 内容

     当然,您可以打开浏览器并敲入地址 [url]http://localhost:8080/[/url] ,以及其它链接(上面已创建)。但本文是PowerShell 的walkthrough 所以我们使用 .NET WebClient classes 来实现这个请求:
 
$webclient   =  New - Object Net.WebClient

$webclient .DownloadString( " [url]http://localhost:8080/[/url] " );

$webclient .DownloadString( " [url]http://localhost:8080/DemoApp[/url] " );

$webclient .DownloadString( " [url]http://localhost:8080/DemoVirtualDir1[/url] " );

$webclient .DownloadString( " [url]http://localhost:8080/DemoApp/DemoVirtualDir2[/url] " );

If  you feeling adventurous you can also use Internet Explorer object itself:

$ie   =  new - object  - com InternetExplorer.Application

$ie .Visible  =   $true

$ie .Navigate(http: // localhost: 8080 / );
 

总结

     本文中,您了解了如何使用PowerShell创建WEB站点, Web Applications, 虚拟目录和应用程序池. 除此之外,我们还使用 PowerShell 创建了一个功能(函数)化的完整方案.
 
     译者注: PowerShell Provider for IIS 7.0 (x86) - CTP2下载连接,请点击这里:)
     
     当然除了上面的方式来管理IIS站点之外,还可以使用Microsoft.Web.Administration(该dll位于"Windows\
System32\inetsrv目录下)。下面的PS脚本就是实现指量创建,删除等站点操作功能的:

function  Setup - IIS
{
    
param ([int] $NumSites )
    
for ( $i = 0  ;  $i   -lt   $NumSites  ;   $i ++ )
    {
        
$Appfolder   =   " c:\inetpub\webroot\Site$i "
        
if  ( -not  (test - path  $Appfolder ))
        {
            md 
$AppFolder   - force  |  out - null
            
$defaultHTM   =   " $Appfolder\default.htm "
            echo 
" This is site $i "   |  out -file   $defaultHTM
        }
    }
}
function  Cleanup - IIS
{
    
param ([int] $NumSites )
    [System.Reflection.Assembly]
:: LoadWithPartialName( " Microsoft.Web.Administration "

    
$mgr   =  new - object Microsoft.Web.Administration.serverManager 

    
for ( $i = 0  ;  $i   -lt   $NumSites  ;   $i ++ )
    {
        
$AppPoolName =   " AppPool_$i "
        
$SiteName   =   " WebSite_$i "
        
$Appfolder   =   " c:\inetpub\webroot\Site$i "
        
$mgr .Sites.Remove( $mgr .Sites[ $SiteName ])
        
$mgr .ApplicationPools.Remove( $mgr .ApplicationPools[ $AppPoolName ])
        
if  (test - path  $Appfolder )
        {
            rd 
$Appfolder   - recurse
        }
    }
    
$mgr .CommitChanges()


function  Makewebs - IIS
{
    
param ([int] $NumSites

    
$start   =  get - date
    [System.Reflection.Assembly]
:: LoadWithPartialName( " Microsoft.Web.Administration " |  out - null 

    
$mgr   =  new - object Microsoft.Web.Administration.serverManager 

    
for ( $i = 0  ;  $i   -lt   $NumSites  ;   $i ++ )
    {
        
$AppPoolName =   " AppPool_$i "
        
$SiteName   =   " WebSite_$i "
        
$portNumber   =   8000   +   $i
        
$mgr .ApplicationPools.Add( $AppPoolName |  out - null
        
$Appfolder   =   " c:\inetpub\webroot\Site$i "  

        
$site   =   $mgr .Sites.Add( $SiteName , $Appfolder , $portNumber )
        
$site .ServerAutoStart  =   $true  

        
$app   =   $site .Applications[ 0 ]
        
$app .ApplicationPoolName  =   $AppPoolName
        
# $site.Bindings.Add("*:$portNumber:", "http") | out-null            
    }
    
$mgr .CommitChanges() 

    
$duration   =  [DateTime] :: Now  -   $start
    write
- host  " Total seconds:  "   +   $duration .TotalSeconds.Tostring()
}
 
     我们只要以“管理员身份”运行Powershell,并在命令行下输入如下(以创建站点为例)代码:   
> Makewebs - IIS  10
 
     这样就会批量创建10web站点了:)
     好了,今天的内容就先到这里了。
     作者: daizhj, 代震军
     Tags: iis7,powershell,powershell provider

0

收藏

daizhenjun

142篇文章,55W+人气,1粉丝