作者:Thomas Deml
在本演练中,你将了解如何创建网站、Web 应用程序、虚拟目录和应用程序池。
介绍
IIS PowerShell 命名空间包含网站、应用、虚拟目录和应用程序池等项。 使用内置的 PowerShell cmdlet 可以轻松创建新的命名空间项并对其进行管理。
创建网站
如果你熟悉 PowerShell,那么你知道 New-Item cmdlet 用于在各种 PowerShell 命名空间中创建新项。 例如,命令 New-Item c:\TestDirectory
创建一个新的文件系统目录(但大多数人将 MD
或 MKDIR
别名用于 New-Item
)。 New-Item
还用于在 IIS PowerShell 命名空间中创建新的网站。
参数
指定目录的名称是创建新文件系统目录时所需的唯一参数。 遗憾的是,创建网站时,这还不够。 创建网站需要其他参数,例如文件系统路径和网络绑定。 下面是创建新网站的命令,后面是 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 参数看起来如此复杂。
使用的构造是哈希表(转到此处了解有关 PowerShell 哈希表的详细信息)。 在哈希表中,键=值对指示反映 IIS 站点绑定部分中的属性的设置:
<bindings>
<binding protocol="http" bindingInformation=":80:TestSite" />
</bindings>
现在,以下是我们使用哈希表的原因:IIS 配置是完全可扩展的(请参阅此处了解更多详细信息),其中包含其他部分和属性。 可以想象有人使用其他属性扩展 <binding> 元素。 哈希表中的键值对可以灵活地纳入这些新属性,而无需完全重写 IIS PowerShell 管理单元。
当然,语法有点复杂。 我们正在考虑包装一些典型任务,例如在以后的技术预览版中使用其他函数或脚本创建网站。
删除站点
下面介绍了如何删除刚刚创建的站点。
PS IIS:\ >Remove-Item IIS:\Sites\TestSite
创建 Web 应用程序
创建 Web 应用程序比创建站点更容易。 让我们开始吧:
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),因为在网站下方,可能需要创建应用程序或虚拟目录。 通过指定 -type 参数,告知 IIS 管理单元创建应用程序。
若要删除应用程序,也可以使用 Remove-Item。
创建虚拟目录
若要创建虚拟目录,还需使用 New-Item cmdlet。 让我们在“默认网站”下创建一个虚拟目录,并在上一步中创建的 Web 应用程序下创建第二个虚拟目录。
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
创建应用程序池
但创建应用程序池更简单。 创建新的 AppPool 只需要指定名称。
PS IIS:\> new-item AppPools\DemoAppPool
Name State
---- -----
DemoAppPool {}
很简单,不是吗? 现在,让我们把这些内容整合到一个端到端方案中。
把这些内容整合到一起
在以下端到端方案中,我们将执行以下步骤:
- 为我们稍后要创建的网站、Web 应用程序和虚拟目录创建一组新的文件系统目录。
- 将一些非常简单的 Web 内容复制到新创建的目录中。
- 创建新的应用程序池
- 创建一个新的站点、一个新的应用程序和两个新的虚拟目录,并将它们分配给新创建的应用程序池。
- 通过 Web 浏览器请求 Web 内容。
步骤 1:创建新目录
我们使用 New-Item cmdlet 创建四个新的文件系统目录。 执行以下命令(如果不想指定 -type 参数,请使用“md”而不是 New-Item):
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
注意
如果 WebAdministration 模块尚未导入,上述 cmdlet 将失败。 为此,可以添加以下 cmdlet 作为包含上述 cmdlet 的脚本的第一步:
Import-Module "WebAdministration"
步骤 4:创建新站点、Web 应用程序和虚拟目录并分配到应用程序池
以下是重点。 创建 DemoSite、DemoApp 和两个虚拟目录 - DemoVirtualDir1 直接位于 DemoSite 下,DemoVirtualDir2 位于 DemoApp 下。 将 DemoSite 和 DemoApp 分配到在上一步中创建的 DemoAppPool。 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 内容
当然,可以打开浏览器并输入 http://localhost:8080/
和所有其他 URL。 但这是一个 PowerShell 演练,我们将使用 PowerShell 通过 .NET WebClient 类执行此操作:
$webclient = New-Object Net.WebClient
$webclient.DownloadString("http://localhost:8080/");
$webclient.DownloadString("http://localhost:8080/DemoApp");
$webclient.DownloadString("http://localhost:8080/DemoVirtualDir1");
$webclient.DownloadString("http://localhost:8080/DemoApp/DemoVirtualDir2");
如果你有冒险精神,也可以使用 Internet Explorer 对象本身:
$ie = new-object -com InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("http://localhost:8080/");
总结
在本演练中,你已了解如何使用 PowerShell 创建网站、Web 应用程序、虚拟目录和应用程序池。 其他 PowerShell 功能用于生成功能性端到端方案。