In this walkthrough you will learn how to create Web-Sites, Web Applications, Virtual Directories and Application Pools.

Introduction

The IIS PowerShell namespace consists of items like Web-Sites, Apps, Virtual Directories and Application Pools. Creating new namespace items and managing them is very easy using the built-in PowerShell cmdlets.

Creating Web-Sites

 If you are familiar with PowerShell you know that the New-Item cmdlet is used to create new items in the various PowerShell namespaces. The command "New-Item c:\TestDirectory" creates a new filesystem directory for example (most people use the "MD" or "MKDIR" alias for New-Item however). New-Item is also used to create new Web-Sites within the IIS 7.0 PowerShell namespace.

Parameters

Specifying the name of the directory is the only argument needed when you create a new file system directory. Unfortunately this is not enough when you create a Web-Site. Additional parameters like the file system path and network bindings are needed to create a Web-Site. Here is the command to create a new Web-Site followed by a dir command:

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 

Using the -physicalPath argument is pretty straightforward. But you might ask yourself why the -bindings argument looks so complex.

The construct used is a hashtable (go here to learn more about PowerShell hash tables). Within the hash table key=value pairs indicate the settings that reflect the attributes within the IIS site bindings section:

<bindings>
        <binding protocol="http" bindingInformation=":80:TestSite" />
</bindings>

Now here is the reason why we use a hash table: IIS configuration is completely extensible (see  here for more details) with additional sections and attributes. You can imagine that somebody extending the <binding> element with additional attributes. Key value pairs within a hash table provide the flexibility to incorporate these new attributes without having to completely rewrite the IIS PowerShell Snap-in.

Granted, the syntax is a bit complex. We are thinking about wrapping some typical tasks like creating sites with additional functions or scripts in a later Tech Preview.

Deleting Sites

Here is how you delete the site you just created.

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

Creating Web Applications

Creating Web Applications is easier than creating sites. Here we go: 

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

 

Name                     ApplicationPool          EnabledProtocols         PhysicalPath
----                     ---------------          ----------------         ------------
DemoApp                  DefaultAppPool           http                     c:\test

The only parameter you have to specify is the type (-type) because underneath a Web-Site you might want to create an Applications or a Virtual Directories. By specifying the -type parameter you tell the IIS Snap-in to create an application.

To delete the application you can also use Remove-Item.  

Creating Virtual Directories

To create a Virtual Directory you also use the New-Item cmdlet. Let's create a Virtual Directory underneath the 'Default Web Site' but and a second one underneath the Web Application we created in the previous step.

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

Creating Application Pools

But it gets even simpler. Creating a new AppPool only requires the name to be specified.

PS IIS:\> new-item AppPools\DemoAppPool

Name                     State
----                     -----
DemoAppPool              {}

Simple, wasn't it? Now let's put this together to an end-to-end scenario.

Putting it all Together

In the following end-to-end scenario we will execute the following step:

  1. Create a set of new file system directories for the sites, web applications and virtual directories we will create a little later.
  2. Copy some very simple web content into the newly created directories.
  3. Create new Application Pool
  4. Create a new site, a new application and two new virtual directories and assign them to newly created Application Pool.
  5. Request the web content via the web browser.

Step 1: Create New Directories

 We use the New-Item cmdlet to create four new file system directories. Execute the following commands (use 'md' instead of New-Item if you don't want to specify the -type parameter):

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

Step 2: Copy Content

Now let's write some simple html content to these directories:

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"

Step 3: Create New Application Pool

Create the new Application Pool 'DemoAppPool' for the new site if you deleted the one we created in the previous sample.  

New-Item IIS:\AppPools\DemoAppPool

Step 4: Create New Sites, Web Applications and Virtual Directories and Assign to Application Pool

Here comes the beef. We create DemoSite, DemoApp and two Virtual Directories - DemoVirtualDir1 is directly underneath DemoSite and DemoVirtualDir2 is underneath DemoApp. We are assigning DemoSite and DemoApp to DemoAppPool created in the previous step. DemoSite is assigned to port 8080 to not conflict with the 'Default Web Site'

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

Voila. All that's left is to request the web content.

Step 5: Request the Web Content

You can of course open the browser and enter http://localhost:8080/ and all the other URLs. But its a PowerShell walkthrough and we'll use PowerShell to do it by using the .NET WebClient classes:

$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");

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/");

Summary

In this walkthrough you learned how to create Web-Sites, Web Applications, Virtual Directories and Application Pools with PowerShell. Additional PowerShell features were used to build a functional end-to-end scenario.

Related Content

보통 ASP.NET 기반의 Web Application을 개발하다 보면, DLL들을 많이 이용하게 되는데,
만일 그 DLL의 일부 코드 수정을 반영 하려면 IIS 서비스를 다시 시작해야 한다.
그래서 보통 IISRESET.EXE 라는 명령어를 이용해 아예 서비스를 내렸다가,
다시 시작하곤 한다.

그러나 단순 DLL의 로직 몇가지 변경 때문에, IISRESET을 하기에는 비용이 비쌀 수 있다.
Web Publishing에 관련 된 대부분의 서비스를 내렸다가 다시 올리는 작업이기 때문에,
Reset 하는 시간도 많이 걸리는데다, IIS를 통해 데이터를 끌어 올리는 비용도 만만치 않다.

이에 IIS 6.0 대 부터는 Application Pool 이라는 것이 있어, 그 Application Pool 만 
정리해 주면 새로운 DLL로 올려줄 수 있도록 제공하게 된다.

보통은 INETMGR을 띄워 해당하는 Application Pool 에서 오른쪽
버튼을 클릭해서 재생(혹은 Recycle)을 선택하면 되는데, UI 도구를 사용하는 것이라, 조작이 좀 귀찮긴 하다.


이 작업을 명령줄로 실행하는 방법을 사용하게 되면, 시작 -> 실행 -> cmd 해서 나오는 도스창(명령 실행창)에서 실행되게 만들거나 Batch 파일로 만들면 더욱 편하게 작업할 수 있다. 

Recycle 할 때 사용하는 명령 줄은 아래와 같다.

IIS 6.0 : cscript //nologo C:\Windows\system32\iisapp.vbs /a "<웹응용프로그램이름>" /r
      예) cscript //nologo C:\Windows\system32\iisapp.vbs /a "SharePoint - 80" /r

IIS 7.0 : C:\Windows\System32\inetsrv\appcmd recycle apppool /apppool.name:"<웹응용프로그램이름>"
      예) C:\Windows\System32\inetsrv\appcmd recycle apppool /apppool.name:"SharePoint - 80"

 

Windows Server 2003에서 Windows Server 2008로 버전이 올라가면서,,

"원격데스크톱 연결"에 두가지 큰 변화가 있습니다.

1. 최대 동시 접속자가 3명에서 2명으로 감소
 - Windows 2003 Server 에서는 최대 3명까지 원격 접속이 가능했습니다.
 - 그러나,, Windows 2008 에서는 최대 동시 접속자가 2명으로 제한됩니다.

2. 한 User로 동시 접속이 안된다?
 - Windows 2008 Server 에서는 기본 설정 상 해당 머신에 동시에 2명 이상이 접속을 시도할 경우
   기존 Session 이 끊겨 버립니다.
  
   2명 이상의 동시 접속을 허용하기 위해서 다음과 같이 진행합니다.
  
  
1. 시작 -> 관리도구 -> 원격 데스크톱 서비스 -> 원격 데스크톱 세션 호스트 구성에서 "사용자당 세션을 하나로 제한" 옵션을 '아니요' 로 변경해 줍니다.


2. '사용자당 세션을 하나로 제한' 옵션을 비활성화 시킨다.


그러면,, 각 머신 당 2명까지의 동시 연결이 가능해집니다.


작업 그룹 이름을(를) 액세스할 수 없습니다. 이 네트워크 리소스를 사용할 권한이 없습니다.


원인
이 문제는 다음 조건 모두에 해당하는 경우에 발생할 수 있습니다.
작업 그룹에 있는 하나 이상의 컴퓨터에 TCP/IP에서 NetBIOS 사용 기능이 설정되어 있지 않습니다.
작업 그룹에 있는 하나 이상의 컴퓨터에서 컴퓨터 브라우저 서비스가 시작되지 않았거나 해제되어 있습니다.

해결 방법
이 문제를 해결하려면 작업 그룹 내의 각 컴퓨터에 TCP/IP에서 NetBIOS 사용 기능이 설정되어 있고 컴퓨터 브라우저 서비스가 실행 중인지 확인하십시오. 이렇게 하려면 다음 단계를 수행하십시오.

1단계: TCP/IP에서 NetBIOS 사용 설정
1. 시작, 제어판을 차례로 누른 다음 네트워크 및 인터넷 연결을 누릅니다.
2. 네트워크 연결을 누릅니다.
3. 로컬 영역 연결을 마우스 오른쪽 단추로 누른 다음 속성을 누릅니다.
4. 인터넷 프로토콜 (TCP/IP)을 누른 다음 속성을 누릅니다.
5. 일반 탭을 누른 다음 고급을 누릅니다.
6. WINS 탭을 누릅니다.
7. NetBIOS 설정에서 TCP/IP에서 NetBIOS 사용을 누른 다음 확인을 차례로 두 번 누릅니다.
8. 닫기를 눌러 로컬 영역 연결 속성 대화 상자를 닫습니다.
9. 네트워크 연결 창을 닫습니다.

2단계: 컴퓨터 브라우저 서비스 시작
1. 시작을 누르고 내 컴퓨터를 마우스 오른쪽 단추로 누른 다음 관리를 누릅니다.
2. 서비스 및 응용 프로그램을 두 번 누릅니다.
3. 서비스를 두 번 누릅니다.
4. 오른쪽 창에서 Computer Browser를 마우스 오른쪽 단추로 누른 다음 시작을 누릅니다.
5. 컴퓨터 관리 창을 닫습니다.


Sometimes you need a local copy of an assembly from the GAC and here is a quick tip on how to do it.  The GAC can be found in the c:\windows\assembly directory, but if you try to browse it, the following custom shell extension appears:

image

This view does not provide the ability to copy assemblies, but it does provide some very useful information such as the strong name details.  Here are some options to get around that feature to copy an assembly from the GAC.

Option 1: Disable the Shell Extension in the Registry

One possibility, but not the best one, is to disable the shell extension.  I don't like this approach because it involves editing the registry and I like the information provided by the shell extension.  Here's how to disable the extension if you want to.  Open the registry editor and add/set the HKLM\Software\Microsoft\Fusion\DisableCacheViewer DWORD value:

image

Set the value to 1:

image

Once you make that change, you can browse the directory:

image

 

Option 2: Go Command-O

Another option is to copy assemblies from the GAC from the command line.  This approach works well if you prefer working from the command line, but if you like to right-click with a mouse, this might not be the choice for you.

I highly recommend PowerShell, but you can use Windows Command Prompt.  Find your way to the c:\windows\assembly directory and copy the file you need:

image

Option 3: Use the SUBST Command

The SUBST command allows you to create a shortcut to a path on your file system and assigns that shortcut a drive letter.  I really like this approach because you have the option of using Windows Explorer without having to disable the shell.

Suppose you want to create a G Drive (G for GAC), use the following command: SUBST G: C:\WINDOWS\ASSEMBLY

image

Then in Windows Explorer you are free to double-click and right-click to your heart's content and still use the shell extension. 

image

+ Recent posts