<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ad hoc Geek &#187; IIS</title>
	<atom:link href="http://www.adhocgeek.com/tag/iis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Simple IIS Scripting</title>
		<link>http://www.adhocgeek.com/2009/08/simple-iis-scripting/</link>
		<comments>http://www.adhocgeek.com/2009/08/simple-iis-scripting/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 13:27:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[JScript]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[windows scripting host]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=93</guid>
		<description><![CDATA[Using the Windows Scripting Host, it&#8217;s possible to write small JScript files which perform administrative tasks. I had a need for a small script to automatically create a virtual directory in IIS and link it with a particular path as part of a (very basic) deployment procedure. After a little bit (maybe 15 minutes?) of [...]]]></description>
			<content:encoded><![CDATA[<p>Using the <a href="http://en.wikipedia.org/wiki/Windows_Script_Host">Windows Scripting Host</a>, it&#8217;s possible to write small <a href="http://en.wikipedia.org/wiki/JScript">JScript</a> files which perform administrative tasks. I had a need for a small script to automatically create a virtual directory in IIS and link it with a particular path as part of a (very basic) deployment procedure. After a little bit (maybe 15 minutes?) of searching and reading, this is what I ended up with :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> install<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> hostname	<span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> projectName	<span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;MyProject&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> projectDir	<span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Projects<span style="color: #000099; font-weight: bold;">\\</span>MyProject&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> existing <span style="color: #339933;">=</span> GetObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;IIS://&quot;</span> <span style="color: #339933;">+</span> hostname <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/W3SVC/1/Root/&quot;</span> <span style="color: #339933;">+</span> projectName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        WScript.<span style="color: #660066;">Echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ERROR : An application called &quot;</span> <span style="color: #339933;">+</span> projectName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; already exists.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//Get the root IIS application</span>
        <span style="color: #003366; font-weight: bold;">var</span> objIIS <span style="color: #339933;">=</span> GetObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;IIS://&quot;</span> <span style="color: #339933;">+</span> hostname <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/W3SVC/1/Root&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> virtualDir <span style="color: #339933;">=</span> objIIS.<span style="color: #660066;">Create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;IISWebVirtualDir&quot;</span><span style="color: #339933;">,</span> projectName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AppIsolated</span>		<span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//pooled process</span>
        virtualDir.<span style="color: #660066;">AccessScript</span> 	<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AppFriendlyName</span>	<span style="color: #339933;">=</span> projectName<span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">Path</span> 		<span style="color: #339933;">=</span> projectDir<span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AccessWrite</span>		<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AccessRead</span>		<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AccessExecute</span>	<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">AppCreate</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        virtualDir.<span style="color: #660066;">SetInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        WScript.<span style="color: #660066;">Echo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;IIS application <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> projectName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> created successfully.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> prop <span style="color: #000066; font-weight: bold;">in</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            msg <span style="color: #339933;">+=</span> prop <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;:&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        WScript.<span style="color: #660066;">Echo</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
install<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pretty simple really. The <a href="http://msdn.microsoft.com/en-us/library/at5ydy31(VS.85).aspx">WScript object</a> is the root of the WSH hierarchy and is always available, but the important type here is <a href="http://msdn.microsoft.com/en-us/library/ms524579.aspx">IISWebVirtualDir</a>.</p>
<p>It&#8217;s possible to do all kinds of things in IIS from a script, but this was all I needed. I might investigate permissions and DNS entries if the deployment gets more complicated (or more frequent).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/08/simple-iis-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

