Sunday, May 4, 2008

An Introduction to IIS 7 PowerShell Provider-Part 2

Continuing our discussion on IIS 7 PowerShell Provider(Didn't read part 1... Check it out at An Introduction to PowerShell Provider for IIS 7 -Part 1), I will be telling you now about the various methods and commands for adding, managing, and deleting IIS websites settings.

Before we hit off with all the funda.. Lets make the appearance of our IIS PowerShell Management Console look similar to that of the usual PowerShell window...

To do this go to the console window->right-click properties ->Colors and enter the below RGB values.

The RGB values for the Screen Background are (1,36,86) and that of the screen text are (238,237,240) ... There we go ... everything looks like the normal PowerShell HUh!!OK Lets start with some real work now...

The 1st thing that I would like to do before we make any change is to know what are the existing websites currently configured...

To get that type this in :

PS IIS:\> Get-ChildItem sites
The Get-Childitem cmdlet gets the items in one or more specified locations. The item would in this case be "sites",which is a container.When Get-Childitem cmdlet is used PowerShell will display all the items inside the current container. As Simple as that!To sum up, output of the command above will list all the websites contained within the sites container. Along with the name, it lists the current state of the sites, id, bindings, and the physical path. Just in case you want to have that saved in a text file ...go for
PS IIS:\> Get-ChildItem sites >c:\sites.txt
FYI, mentioning c:\ before infotxt is necessary, hope that is obvious enough to everyone as we are currently working from the IIS drive. Anyway's, c:\ can be replaced with any drive partition of your choice.

-->To add a new website, type this in

New-Item iis:\Sites\NewWebsite -bindings @{protocol="http";bindingInformation=":80:NewWebsite"} -physicalPath c:\NewWebsite
If you check out your applicationHostconfig (%systemdrive%\Windows\System32\inetsrv\config) now, you will find that the below code is added.

<site name="NewWebsite" id="3" ServerAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="c:\NewWebsite" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":80:NewWebsite" />
</bindings>
</site>
Your site will now be listed in IIS Manager, although you might have to refresh IIS Manager in a few cases if it was already open. However, the above command WILL NOT create c:\NewWebsite if it is not present. You can also custom configure "id" of the website, all you got to do is add the -id parameter at the last of the New-Item command along with the id that you wish. If there is duplication in any of the parameters, PowerShell will not continue and throws an error along with the reason.

Lets continue and quickly create a new application in our NewWebsite. Here's how you do it:

PS IIS:\> New-Item IIS:\Sites\NewWebsite\NewApp -physicalPath c:\NewWebsite -type
Follow up on how we are using the New-Item cmdlet to create different items within IIS. Again you need to remember that it will not check if there is any folder or content that is actually present at the physical location. Why is that?

Well, here is why. Because New-Item creates a new item in a namespace. The type of items that can be created depends upon the Windows PowerShell provider used. The provider we are dealing with is IIS7 Provider.

ApplicationHost.config will now have the following section added under the site tag for the NewWebsite section.

<application path="/NewApp">
<virtualDirectory path="/" physicalPath="c:\NewWebsite" />
</application>

To create a Virtual directory just interchange the type from Application to VirtualDirectory in the above command.

But what is the difference between an Application and a Virtual Directory? This is something that I have been asked a lot of times. So, finally here comes the answer in writing ;)

An application is a unit that contains files within single or multiple virtual directories that works over different protocols to provide various web services. On the other hand, a virtual directory is a file directory is that contains various content files for a website.

If you want more info on this, check these out

-->Did all that sound confusing?? No problem, let's get to the simpler world now

What about application pools one may ask. Simple:

PS IIS:\> new-item AppPools\NewAppPool
Next, removing a website is even simpler than adding a website. All you got to do is,

Remove-Item iis:\Sites\"Name of the Website to be removed"

and that's it. However, you will have to remove the content manually, that will not be taken care by the above command.

Ok that is it for Part 2, keeping checking as I will soon come out with part 3 and more interesting stuff.

If you have any questions or comments... please do write it down and I will try to answer them asap.

0 comments: