Access DFS from .NET posted

June 29, 2004

[UPDATE 09 Feb 2005: I added the sample code from this post to the NetDfsAdd definition at pinvoke.net]

[UPDATE 29 Dec 2004: I added some C# code for utilizing NetDfsAdd at the end of this post]

As an IT developer, I'm subject to the downtime schedule of my customers.  This means that I'm often informed (usually around holidays) of an upcoming change that will affect some of my many automation tasks.  This 4th of July is no exception.

I have less than two days to modify, test, and deploy four production scripts to account for several important server changes and a new dependency on Microsoft's Distributed File System (DFS).  My biggest challenge is taking legacy VBScript code and adding the ability to create DFS links.  The issue with using VBScript is that there's no p/invoke ability to use the DFS APIs in netapi32.dll, and no native COM wrapper for DFS.  I can't wrap netapi32 myself due to a dispersed install base, hatred of COM, and lack of time.

I'm resigned to shelling out (via the Exec method in WSH 5.6) to dfscmd.exe.  While it's frustrating to not have the time to dig a bit deeper, the legacy scripts must get moved over quickly and continue to work reliably over time.

How would I like to see it done?  I'd like to write an XML Web Service in C# that would p/invoke NetDfsAdd to create the link.  I'd configure Kerberos delegation and enable impersonation so that anyone in the enterprise who already has native permissions would be able to use the web service from any OS.  I'd add MS SQL logging of all transactions, including errors, so that I could track usage and trend success rate over time.  And I'd do this for every little task like this until I don't have to write code anymore.

Here is some C# code which might work if you wish to call NetDfsAdd from .NET:

[DllImport("Netapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern int NetDfsAdd(
 [MarshalAs(UnmanagedType.LPWStr)]
 string DfsEntryPath,
 [MarshalAs(UnmanagedType.LPWStr)]
 string ServerName,
 [MarshalAs(UnmanagedType.LPWStr)]
 string PathName,
 [MarshalAs(UnmanagedType.LPWStr)]
 string Comment,
 int Flags);
 
void TestMethod(void) {
 // DFS_ADD_VOLUME has a value of 1
 NetDfsAdd("\\DfsRoot\Path\Dir", "FileServer", "Share", "Comment", 1);
}
Published Saturday, December 15, 2007 10:31 PM by jkhines
Filed under:

Comments

# siliconpizza.com » Blog Archive » C# admin for Distributed file System link

Pingback from  siliconpizza.com  » Blog Archive   » C# admin for Distributed file System link