Just thought of sharing a some useful IIS App pool functions I wrote long back in C#. Requirement was to play around with IIS v5.0/6.0 and do all settings dynamically -
Note : You must have administrator priveledge to perform all IIS actions.
Namespace Required
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.IO;
using System.Diagnostics;
using IISOle; // IISOle requires a reference to the Active Directory Services.
using System.Reflection;
using System.Configuration;
using System.Text.RegularExpressions;
Functions
public static bool RecycleAppPool(string serverName, string adminUsername, string adminPassword, string appPoolName)
{
DirectoryEntry appPools = new DirectoryEntry("IIS://" + serverName + "/w3svc/apppools", adminUsername, adminPassword);
bool status = false;
foreach (DirectoryEntry AppPool in appPools.Children)
{
if (appPoolName.Equals(AppPool.Name, StringComparison.OrdinalIgnoreCase))
{
AppPool.Invoke("Recycle", null);
status = true;
break;
}
}
appPools = null;
return status;
}
/// <summary>
/// Creates AppPool
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="appPoolName"></param>
public static bool CreateAppPool(string metabasePath, string appPoolName)
{
// metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
// for example "IIS://localhost/W3SVC/AppPools"
// appPoolName is of the form "<name>", for example, "MyAppPool"
DirectoryEntry newpool, apppools;
try
{
if (metabasePath.EndsWith("/W3SVC/AppPools"))
{
apppools = new DirectoryEntry(metabasePath);
newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
newpool.CommitChanges();
newpool = null;
apppools = null;
return true;
}
else
throw new Exception(" Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.");
}
catch (Exception ex)
{
throw new Exception(string.Format("Failed in CreateAppPool with the following exception: \n{0}", ex.Message));
}
finally
{
newpool = null;
apppools = null;
}
}
/// <summary>
/// Assigns AppPool to Virtual Directory
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="appPoolName"></param>
public static bool AssignVDirToAppPool(string metabasePath, string appPoolName)
{
// metabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
// for example "IIS://localhost/W3SVC/1/Root/MyVDir"
// appPoolName is of the form "<name>", for example, "MyAppPool"
//Console.WriteLine("\nAssigning application {0} to the application pool named {1}:", metabasePath, appPoolName);
DirectoryEntry vDir = new DirectoryEntry(metabasePath);
string className = vDir.SchemaClassName.ToString();
if (className.EndsWith("VirtualDir"))
{
object[] param = { 0, appPoolName, true };
vDir.Invoke("AppCreate3", param);
vDir.Properties["AppIsolated"][0] = "2";
vDir = null;
return true;
}
else
throw new Exception(" Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools");
}
/// <summary>
/// Delete AppPool
/// </summary>
/// <param name="metabasePath"></param>
/// <param name="appPoolName"></param>
public static bool DeleteAppPool(string serverName, string adminUsername, string adminPassword, string appPoolName)
{
// metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
// for example "IIS://localhost/W3SVC/AppPools"
// appPoolName is of the form "<name>", for example, "MyAppPool"
DirectoryEntry appPools = new DirectoryEntry("IIS://" + serverName + "/w3svc/apppools", adminUsername, adminPassword);
bool status = false;
foreach (DirectoryEntry AppPool in appPools.Children)
{
if (appPoolName.Equals(AppPool.Name, StringComparison.OrdinalIgnoreCase))
{
AppPool.DeleteTree();
status = true;
break;
}
}
appPools = null;
return status;
}
Hope you folks find it useful. If you need any other help related to IIS, just post me your requirement.
Yesterday, I was working on an application where we have to store unicode characters in existing database. So our requirement was to switch all varchar column within the database to Nvarchar. There were around 22 tables and manually changing each one would be tedious task.
So here is what we did -
DECLARE @T VARCHAR(255) /*Declared variable to store table name */
DECLARE @C VARCHAR(500) /*Declared variable to store column name */
DECLARE @D VARCHAR(500) /*Declared variable to store datatype name */
DECLARE @L VARCHAR(50) /*Declared variable to store datatype length */
DECLARE @N varchar(25) /*Declared variable to identify column is nullable or not */
DECLARE Table_Cursor CURSOR FOR
SELECT
[A].[Name],
[B].[Name],
case([B].[Xtype]) when 167 then 'NVarchar' end as [DataType],
'(' + cast( ([B].[length]) as varchar) + ')' as Length,
case([B].[IsNullable]) when 1 then ' null' when 0 then '' end as IsNullable
FROM
Sysobjects AS [A], Syscolumns AS [B]
WHERE [A].[ID] = [B].[ID] AND
[A].[XType] = 'U' /* Table (User-Defined) */ AND ( [B].[XType] = 167 /* VARCHAR */)
--[B].[XType] = 35 /* TEXT */ OR
--[B].[XType] = 231 /* SYSNAME */ OR
--[B].[XType] = 99 /* NTEXT */ OR
and [B].Length <=4000 /*NVarchar max length is 4000 anything above that will trim */
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T,@C,@D,@L,@N
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec ('Alter Table [' + @T + '] Alter column [' + @C + '] [' + @D + '] ' + @L + ' ' + @N)
FETCH NEXT FROM Table_Cursor INTO @T,@C,@D,@L,@N
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
Select the code snippet and just press f5 in query analyzer.
Hope this saves your time too...