﻿# 如何才能在 IIS 7.5 使用 Windows PowerShell Snap-In 功能

在 IIS 7.0 的時候，由於需要額外安裝 [Windows PowerShell Snap-In for IIS 7.0](http://www.iis.net/download/PowerShell) 才能在 PowerShell 裡管理 IIS 7 的相關站台與設定，而且安裝好之後還會在「程式集」中建立一個 IIS PowerShell Management Console 的捷徑，開啟之後就可以立即開始使用這些好用的 Cmdlet。不過此功能到了 IIS 7.5 已經內建在作業系統裡，感覺好像是件好事，不過它卻把 Snap-In 的捷徑移除了，我在不熟悉 Windows PowerShell 的時候還真的找不到開啟的方法，費了些功夫才研究出來。

在 Windows Vista / Windows Server 2008 時，在安裝過 [Windows PowerShell Snap-In for IIS 7.0](http://www.iis.net/download/PowerShell) 之後，在桌面就會出現一個如下圖的捷徑，非常方便：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/85f417c719bc350d3632-2011-6-27____10-03-54.png)

到了 Windows 7 / Windows Server 2008 R2 之後，沒辦法讓你安裝 [Windows PowerShell Snap-In for IIS 7.0](http://www.iis.net/download/PowerShell) (因為已經內建)，如果想安裝 [Windows PowerShell Snap-In for IIS 7.0](http://www.iis.net/download/PowerShell) 還會告訴你不能安裝，殘念～

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/51bbdb87ffb9f2af117c-SNAGHTML9a0c6e9.png)

其實真相是，IIS 7.5 已經不把這東西當成是 Snap-in 了，而是改用 WebAdministration 模組來替代，原因不明。所以在開啟 Windows PowerShell 視窗後要能使用這些方便的 Cmdlet 會需要手動匯入 WebAdministration 模組才行，這個匯入的動作沒人跟你講，你可能會找半天還找不到，我第一次使用時就花了不少時間才研究出來，但其實我要的是一個方便開啟的捷徑而已！

若要手動載入 WebAdministration 模組，你知道最難的的地方在哪裡嗎？答案就是：**你必須知道模組名稱是 WebAdministration**！上網查 IIS.net 的文件，所有關鍵字都是 Windows PowerShell Snap-In (當時我也不知道原來 IIS 7.5 已經換成用 Module 的方式載入)，從官網也看不出原來要載入的是 WebAdministration 模組，所以就會開始卡關卡一段時間，直到找到相關資料為止。@\_@''

所以我先介紹一個重要的指令，可以列出在你電腦裡可用的模組有哪些：

```
Get-Module -ListAvailable
```

[![image](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/1f7faf49f14d5d8f697b-image_thumb_65.png "image")](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/d2c4b2871eabfcd2a407-image_926.png)

備註：WebAdministration 模組的所在位置如下：

```
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\WebAdministration
```

所以，當你開啟 Windows PowerShell 之後，必須手動先匯入 WebAdministration 模組，指令如下：

```
Import-Module WebAdministration
```

  ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/e4b37924e07dbe4433cc-image_927.png)

如果你想知道 WebAdministration 模組包含哪些 Cmdlet 或 Alias 可用，也可輸入以下指令查詢：

```
get-command -pssnapin WebAdministration
```

  ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/ec7b303858b19fa0cc55-image_928.png)

當 WebAdministration 模組匯入之後，就會在你的 PowerShell 環境建立一個 **IIS:\\** 命名空間，你可以輸入 **IIS:** 即可進入，並瀏覽 IIS 相關資訊：

  ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/206cd3fad09fae9fd606-image_929.png)

最後，我再補充一個建立捷徑的方法：

1\. 新增捷徑

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/cf5a0afe460c423256fc-image_930.png)

2\. 輸入以下指令與參數

C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -command Import-Module WebAdministration;set-location IIS:\\;clear-host

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/1bc93942c49fb25b1dcd-SNAGHTMLa8e0c56.png)

3\. 設定捷徑名稱 **IIS PowerShell Management**

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/7348b59258d67944af95-SNAGHTMLa8a6c31.png)

大功告成!

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/07c642b06d9eed96f1d3-image_931.png)

接下來就是要進一步瞭解如何透過 PowerShell 進行站台管理，詳細文章請參考 [Using Scripts to Automate Management](https://web.archive.org/web/20120902173757/http://learn.iis.net:80/page.aspx/98/using-scripts-to-automate-management) 頁面的說明。

**相關連結**

-   [Using Scripts to Automate Management](https://web.archive.org/web/20120902173757/http://learn.iis.net:80/page.aspx/98/using-scripts-to-automate-management)
-   [Windows PowerShell Snap-In : The Official Microsoft IIS Site](http://www.iis.net/download/PowerShell)
-   [PowerShell: Load WebAdministration in ps1 script on both IIS 7 and IIS 7.5](http://stackoverflow.com/questions/1924217/powershell-load-webadministration-in-ps1-script-on-both-iis-7-and-iis-7-5)
-   [PowerShell Snap-in: Navigating the IIS Snap-in Namespace](https://learn.microsoft.com/en-us/iis/manage/powershell/powershell-snap-in-navigating-the-iis-snap-in-namespace)
-   [Installing and Using the Windows PowerShell Snap-In for IIS 7.5](https://learn.microsoft.com/en-us/iis/manage/powershell/installing-the-iis-powershell-snap-in) (一段很不清楚的影片)
-   [使用 Windows PowerShell 撰寫指令碼](http://technet.microsoft.com/zh-tw/scriptcenter/dd742419.aspx)
