Archive for November, 2011

IIS 7 Permissions – Application Pool Identity User Accounts

In IIS7- when using app pools which use the app pool identity, you can find the user account (for use with ACL) in the “IIS AppPool” domain- so you can reference the app pool accounts with IIS AppPool\<app pool name>

More info can be found here; http://learn.iis.net/page.aspx/624/application-pool-identities/

No Comments

Grabbing data from the registry on a remote machine using dot net and WMI

I recently had to write an app which, given a computer name, would grab their telephone extension from a registry key (For the purpose of the code example below, I just grab the CommonFilesDir key from the windows node. There’s a couple of ways you can achieve this- either using RegistryKey.OpenRemoteBaseKey which gave me a whole bunch of permissions issues, plus you need the remote registry and remote administration services active on the server/client OR you can use the Windows Management Instrumentation (WMI) service. I took this route, as it’s a service which is active by default on all of our machines on the domain, and it was easier to get the permissions right.

Before I paste the code, a GOTCHA to beware of, straight from the MSDN here;

The registry provider is hosted in LocalService—not the LocalSystem. Therefore, obtaining information remotely from the subtree HKEY_CURRENT_USER is not possible

ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
options.Username = "<domain_admin_username>";
options.Password = "<domain_admin_password>";

// http://msdn.microsoft.com/en-us/library/system.management.managementscope.aspx
ManagementScope ms = new ManagementScope("\\\\<computer name>\\root\\default", options);          
   
ms.Connect();

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa390788(v=vs.85).aspx
ManagementClass mc = new ManagementClass("stdRegProv");
mc.Scope = ms;

ManagementBaseObject inParams = mc.GetMethodParameters("GetStringValue"); 
// there are other methods for grabbing other reg types- see 
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa393664(v=VS.85).aspx

inParams["hDefKey"] = RegHive.HKEY_LOCAL_MACHINE;
inParams["sSubKeyName"] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
inParams["sValueName"] = "CommonFilesDir";

ManagementBaseObject outParams =
        mc.InvokeMethod("GetStringValue", inParams, null);

if (outParams["ReturnValue"].ToString() == "0")
{
    Console.WriteLine(outParams["sValue"]);
}
else
{
    Console.WriteLine("Error " + outParams["ReturnValue"] + " please refer to http://msdn.microsoft.com/en-us/library/ms681382%28v=3Dvs.85%29.aspx");
}

Console.ReadLine();

This code requires this enum..

public enum RegHive : uint
{
  HKEY_CLASSES_ROOT = 0x80000000,
  HKEY_CURRENT_USER = 0x80000001,
  HKEY_LOCAL_MACHINE = 0x80000002,
  HKEY_USERS = 0x80000003,
  HKEY_CURRENT_CONFIG = 0x80000005
}

If you receive a COM error back “The RPC server is unavailable”, make sure the target machine’s firewall isn’t blocking the call- this will open the port;
netsh firewall set service RemoteAdmin

No Comments

Grouped pagination links function in JavaScript

I just created a JavaScript port of an awesome bit of pagination code I first used years and years ago while working in Classic ASP, originally written by Dave Child who I was working with at the time. The original can be found on this site here.

It’s an awesome script when you have 100′s of pages of data (which probably suggests you need to implement some good filtering and search features to save your poor users..) and presents the links like so..

« Prev 1 2 3 … 7 [8] 9 … 208 209 210 Next »

function BuildGroupedPagination(current_page, total_pages, base_url, seperator) 
{ 
	var url = base_url; 
	var strPages = ""; 
	var intMaxPages = 0;
	var intMinPages = 0;
	var intPaginI = 0;

    if (typeof(seperator) == 'undefined')
    {
        seperator = " ";
    }

	if (total_pages > 10) 
	{ 
		if (total_pages > 3) 
		{ 
			intMaxPages = 3;
		}
		else 
		{
			intMaxPages = total_pages;
		} 

		for (intPaginI = 1; intPaginI <= intMaxPages; intPaginI++) 
		{

			if (intPaginI == current_page) 
			{ 
				strPages += "<strong>[" + intPaginI + "]</strong>";
			}
			else 
			{
				strPages += "<a href=\"" + url + intPaginI + "\">" + intPaginI + "</a>";
			} 

			if (intPaginI < intMaxPages) 
			{ 
				strPages += seperator;
			} 
		} 

		if (total_pages > 3) 
		{ 
			if ((current_page > 1) && (current_page < total_pages)) 
			{ 
				if (current_page > 5) 
				{ 
					strPages += " ... ";
				}
				else 
				{
					strPages += seperator;
				} 

				if (current_page > 4) 
				{ 
					intMinPages = current_page;
				}
				else 
				{
					intMinPages = 5;
				} 

				if (current_page < total_pages - 4) 
				{ 
					intMaxPages = current_page;
				}
				else  
				{
					intMaxPages = total_pages - 4;
				} 

				for (intPaginI = intMinPages - 1 ; intPaginI <= intMaxPages + 1; intPaginI++) 
				{
					if (intPaginI == current_page) 
					{ 
						strPages += "<strong>[" + intPaginI + "]</strong>";
					}
					else 
					{
						strPages += "<a href=\"" + url + intPaginI + "\">" + intPaginI + "</a>";
					}

					if (intPaginI < intMaxPages + 1) 
					{ 
						strPages += seperator;
					}
				}

				if (current_page < total_pages - 4) 
				{ 
					strPages += " ... ";
				}
				else 
				{
					strPages += seperator;
				} 
			}
			else 
			{
				strPages += " ... ";
			} 

			for (intPaginI = total_pages - 2; intPaginI <= total_pages; intPaginI++) {
				if (intPaginI == current_page) { 
					strPages += "<strong>[" + intPaginI + "]</strong>";
				}
				else {
					strPages += "<a href=\"" + url + intPaginI + "\">" + intPaginI + "</a>";
				} 

				if (intPaginI < total_pages) { 
					strPages += seperator;
				} 
			} 
		}
	}
	else 
	{
		for (intPaginI = 1; intPaginI <= total_pages; intPaginI++) 
		{
			if (intPaginI == current_page) 
			{ 
				strPages += "<strong>" + intPaginI + "</strong>"; 
			}
			else 
			{
				strPages += "<a href=\"" + url + intPaginI + "\">" + intPaginI + "</a>";
			} 

			if (intPaginI < total_pages) 
			{ 
				strPages += seperator;
			}
		}
	}
	return strPages;
}

6 Comments

Open Windows Firewall ports for SQL Server 2008

http://support.microsoft.com/kb/968872

As noted in the article, you can run this batch file;

@echo =========  SQL Server Ports  ===================
@echo Enabling SQLServer default instance port 1433
netsh firewall set portopening TCP 1433 "SQLServer" 
@echo Enabling Dedicated Admin Connection port 1434
netsh firewall set portopening TCP 1434 "SQL Admin Connection" 
@echo Enabling conventional SQL Server Service Broker port 4022  
netsh firewall set portopening TCP 4022 "SQL Service Broker" 
@echo Enabling Transact-SQL Debugger/RPC port 135 
netsh firewall set portopening TCP 135 "SQL Debugger/RPC" 
@echo =========  Analysis Services Ports  ==============
@echo Enabling SSAS Default Instance port 2383
netsh firewall set portopening TCP 2383 "Analysis Services" 
@echo Enabling SQL Server Browser Service port 2382
netsh firewall set portopening TCP 2382 "SQL Browser" 
@echo =========  Misc Applications  ==============
@echo Enabling HTTP port 80 
netsh firewall set portopening TCP 80 "HTTP" 
@echo Enabling SSL port 443
netsh firewall set portopening TCP 443 "SSL" 
@echo Enabling port for SQL Server Browser Service's 'Browse' Button
netsh firewall set portopening UDP 1434 "SQL Browser" 
@echo Allowing multicast broadcast response on UDP (Browser Service Enumerations OK)
netsh firewall set multicastbroadcastresponse ENABLE 

No Comments

Executing sql statements from the command line (SQL Server 2008 R2 Express)

One of the things I’ve always liked about MySql is its easy to use command line interface. Microsoft Enterprise Manager was always pretty good, and of course SQL Management Studio, so I rarly have to interface with MSSQL using the command line, but when I did, I always used osql. As of SQL 2008 this was replaced by sqlcmd which is very similar to the mysql command line implementation. Basic usage is;

sqlcmd -S server_name\instance_name -U sa -P your_password

You can then type directly into the console, executing a “GO” after each statement to run. I recently used this to enable remote connections on a new install of sql express, like so;

1> exec sys.sp_configure N'remote access', N'1'
2> GO
Configuration option 'remote access' changed from 0 to 1. Run the RECONFIGURE statement to install.
1> RECONFIGURE WITH OVERRIDE
2> GO
1> exit

No Comments