Easy Way to GRANT/REVOKE Rights to Tables within a Microsoft SQL Server Database

Yesterday, a customer asked for help setting up an additional database user with only SELECT rights to his database.  He had a lot of tables, so the easiest solution I came up with involved using the sp_MSforeachtable system stored procedure.  Here’s the T-SQL code:

EXEC sp_MSforeachtable
'REVOKE ALL ON ? FROM username'

EXEC sp_MSforeachtable
'GRANT SELECT ON ? TO username'

Make sure you replace username with the database username that you want the rights applied to.

PowerShell Script to Restart the Web Management Service

It’s been over a year since I last wrote an article partly because I didn’t know what I wanted to blog about and partly because of the lack of time. Well, the other day I found myself working on a problem to simplify restarting the Web Management Service on a remote machine when it gets stuck (i.e. hangs and goes into a Stopping state) and felt that the script might be useful to other Windows system administrators out there. So here it is:

$ComputerName = Read-Host -Prompt 'Enter computer name or IP address: '
$Username = Read-Host -Prompt 'Enter your username: '
$Password = Read-Host -Prompt 'Enter your password: '
$ProcessId = Get-Process -ComputerName "$ComputerName" WMSvc | Select -Expand Id
taskkill.exe /S "$ComputerName" /U "$Username" /P "$Password" /IM "$ProcessId" /F

All you need to do is enter the computer name or IP address of the remote computer and your Active Directory Domain login credentials after running the script, and it will do the rest.

Custom Headers for Content-Type in the web.config file? Don’t do it.

Life’s about lessons learned, and here is one lesson I learned that I hope will save someone else from hours of frustration.  As I scoured the web looking for ways to improve the SEO of one of my sites and conform to standards, I must have run across some article, post, or tip which suggested I add this to the web.config file:

<configuration>
  <httpprotocol>
    <customheaders>
      <add name="Content-Type" value="text/html; charset=utf-8"></add>
    </customheaders>
  </httpprotocol>
</configuration>

I probably did it because I didn’t want to type out:

<head>
  <meta name="Content-Type" value="text/html; charset=utf-8" />
</head>

on every page and because of maintenance reasons.  Well, everything was fine and dandy until I decided to convert one of my .aspx pages to .html and write my own AJAX code.  I didn’t even notice the problem until I tried pulling up my site in Internet Explorer as I do most of my testing on Google Chrome.  Everything displayed fine on Google Chrome, but on Internet Explorer, it broke the CSS of the page and was asking you if you wanted to open or save the default document instead of rendering it.  I pulled my hair out researching and troubleshooting the problem (checked to see if it was a browser compatibility issue, due to caching, settings enabled/disabled, etc.) and almost gave up, but I finally figured it out when I reviewed the console from Google Chrome’s Developer tools and noticed I was getting these strange error messages:

Resource interpreted as Image but transferred with MIME type text/ html
Resource interpreted as Script but transferred with MIME type text/ html
Resource interpreted as Stylesheet but transferred with MIME type text/ html

It was adding HTTP headers to all my links which caused Internet Explorer to interpret them incorrectly.  Once I removed it, my site rendered correctly again on Internet Explorer.  I also learned there is a correct way of adding HTTP headers for charset according to W3C.

For ASP and ASP.Net pages, you can add this line to the page:

< %Response.charset="utf-8"%>

Or you can add this to the web.config file:

<configuration>
  <system .web>
    <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="auto" uiCulture="auto"></globalization>
  </system>
</configuration>