In my on-going attempts to simplify the deployment of our web application here at Day Job, I have created a PowerShell script that sets the permissions on all the directories used by the app. It’s amazing how much simpler something like this is in PowerShell compared to VBScript. Here’s some code that will give the NetworkService user read-access to a directory (useful for data files that your web app needs to access):
You need to pass in enumeration members to insure that the permission change is applied correctly, so the first block of code just grabs the needed values from the appropriate enumerations. With those in hand, all you have to do is use the FileSystemAccessRule class to add a new rule, then apply it to the directory.
One tricky thing I ran into was handling the Internet Guest Account. Recall from my WiX ordeals that IIS 7.0 systems (Vista and Windows 2008) use IIS_USRS while IIS 6 systems use IUSR_[MachineName]. I couldn’t come up with an elegant way to handle it, but this hackery does the trick:
The above snippet Grants the NetworkService and IIS guest account read permissions on the Web directory, which will allow our ASP.NET located in Web to execute once the virtual directory is created in IIS (which I’ll describe how to do just as soon as I figure it out).