Programmatically setting the "execute permissions" for a virtual directory to "Scripts Only"

I was wondering if anyone knew the property that you can set to make a virtual directory's execute permissions set to scripts only. I am trying to automatically create multiple virtual directories within a custom installation process.

I'm using System.DirectoryServices:

// Code chunk
            DirectoryEntries virtualEntries = rootIIS.Children;

            // now add each virtual
            DirectoryEntry virtualDir = null;

            try
            {
                virtualDir = rootIIS.Children.Find(appName, "IIsWebVirtualDir");
            }
            catch { /* do nothing  */ }

            if (virtualDir == null)
                virtualDir = rootIIS.Children.Add(appName, "IIsWebVirtualDir");
           
            virtualDir.CommitChanges();

            virtualDir.Properties["AppRoot"][0] = "/LM/W3SVC/1/ROOT/" + appName;
            virtualDir.Invoke("AppCreate2", new object[] { "2" });
            virtualDir.CommitChanges();
            virtualDir.Properties["Path"][0] = path;
            virtualDir.Properties["EnableDefaultDoc"][0] = true;
            virtualDir.Properties["DefaultDoc"][0] = defaultDocName;
            virtualDir.Properties["AppFriendlyName"][0] = appName;
            virtualDir.Properties["AccessRead"][0] = true;
            virtualDir.Properties["AccessExecute"][0] = false;
            virtualDir.Properties["AccessScript"][0] = true;
            virtualDir.Properties["EnableDirBrowsing"][0] = false;
            virtualDir.Properties["AuthAnonymous"][0] = true;
            virtualDir.Properties["AuthBasic"][0] = false;
            virtualDir.Properties["AuthNTLM"][0] = false;
            virtualDir.Properties["EnableDefaultDoc"][0] = true;
            virtualDir.Properties["DefaultDoc"][0] = defaultDocName;
// End Code Chunk

Is it a property available to me within the Properties of a Virtual Directory(Directory Entry) object? The virtual directories are all created perfectly, but the execute permissions are not being properly set to scripts only. Any ideas?

Thanks!
i-mate05Asked:
Who is Participating?
 
Chris DentPowerShell DeveloperCommented:

Hi,

It's an attribute called AccessFlags you need to alter. It has a number of values available. Please forgive any basic mistakes in the code, I never learnt anything but VbScript. There are a number of constants that go with it to build up to the value you end up writing:

Const MD_ACCESS_EXECUTE = 4
Const MD_ACCESS_SOURCE = 16
Const MD_ACCESS_READ = 1
Const MD_ACCESS_WRITE = 2
Const MD_ACCESS_SCRIPT = 512
Const MD_ACCESS_NO_REMOTE_EXECUTE = 8192
Const MD_ACCESS_NO_REMOTE_READ = 4096
Const MD_ACCESS_NO_REMOTE_WRITE = 1024
Const MD_ACCESS_NO_REMOTE_SCRIPT = 16384

The most likely setting here is going to be Read and Script:

virtualDir.Properties["accessFlags"][0] = MD_ACCESS_READ + MD_ACCESS_SCRIPT

In VbScipt it would be:

Set objVirtualDirectory = GetObject("IIS://Server/W3SVC/<SiteID>/ROOT")

objVirtualDirectory.Put "accessFlags", MD_ACCESS_READ + MD_ACCESS_SCRIPT
objVirtualDirectory.SetInfo

HTH

Chris
0
 
i-mate05Author Commented:
Thanks for the informative response, I can successfully set the execute permissions to scripts only when applying this through VBScript, however I still cannot apply this setting through .NET's System.DirectoryServices... I'll post another thread on this issue. Thanks for the help!
0
 
kstoreyCommented:
This definately can be achieved using system.directoryservices.directoryentry
It looks as though you're committing the setitngs prior to the script access being set (you definately use  virtualDir.Properties["AccessScript"][0] = true;)
Try this instead:
           virtualDir.Properties["AppRoot"][0] = "/LM/W3SVC/1/ROOT/" + appName;
            virtualDir.Properties["Path"][0] = path;
            virtualDir.Properties["EnableDefaultDoc"][0] = true;
            virtualDir.Properties["DefaultDoc"][0] = defaultDocName;
            virtualDir.Properties["AppFriendlyName"][0] = appName;
            virtualDir.Properties["AccessRead"][0] = true;
            virtualDir.Properties["AccessExecute"][0] = false;
            virtualDir.Properties["AccessScript"][0] = true;
            virtualDir.Properties["EnableDirBrowsing"][0] = false;
            virtualDir.Properties["AuthAnonymous"][0] = true;
            virtualDir.Properties["AuthBasic"][0] = false;
            virtualDir.Properties["AuthNTLM"][0] = false;
            virtualDir.Properties["EnableDefaultDoc"][0] = true;
            virtualDir.Properties["DefaultDoc"][0] = defaultDocName;
            virtualDir.Invoke("AppCreate2", new object[] { "2" });
            virtualDir.CommitChanges();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.