20 November 2009

Set NTFS folder permissions on remote machine using .NET and WMI

After considerable amount of efforts and trial I was finally able to hack the solution to set NTFS permissions on a remote machine using .NET. For local file system it is just couple of line of codes and it works like a charm but when it comes to remote machines it just gives you a nightmare . So here goes my solution. I am sure this will help someone.

public bool SetPermissions(string remoteDir, string MachineName, string UserName, string Password) // Username and Password is of Admin account on remote machine
{
string TempName = remoteDir;
int Index = TempName.IndexOf(":");
string DriveLetter = ConfigurationManager.AppSettings["ShareDriveLetter"]; // e.q. C
if (Index != -1)
{
string[] arr = TempName.Split(new char[] { ':' });
DriveLetter = arr[0];
TempName = TempName.Substring(Index + 2);
}

ManagementPath myPath = new ManagementPath();
myPath.NamespacePath = @"root\CIMV2";

ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = UserName;
oConn.Password = Password;
oConn.EnablePrivileges = true;
myPath.Server = MachineName;

ManagementScope scope = new ManagementScope(myPath, oConn);
scope.Connect();
//without next strange manipulation, the os.Get().Count will throw the "Invalid query" exception
remoteDir = remoteDir.Replace("\\", "\\\\");
ObjectQuery oq = new ObjectQuery("select Name from Win32_Directory where Name = '" + remoteDir + "'");
using (ManagementObjectSearcher os = new ManagementObjectSearcher(scope, oq))
{
if (os.Get().Count == 0) //It don't exist, so create it!
{
ManagementPath path2 = new ManagementPath();
path2.Server = MachineName;
path2.ClassName = "Win32_Process";
path2.NamespacePath = @"root\CIMV2";
ManagementScope scopeProcess = new ManagementScope(path2, oConn);

using (ManagementClass process = new ManagementClass(scopeProcess, path2, null))
{
//Command line that you want to execute on remote machine
string commandLine = @"cmd /C cacls " + TempName +" /C /T /E /P "+"0909020:F"; // 0909020:F is username:permissions

using (ManagementBaseObject inParams = process.GetMethodParameters("Create"))
{
inParams["CommandLine"] = commandLine;
inParams["CurrentDirectory"] = DriveLetter + @":\\";
inParams["ProcessStartupInformation"] = null;
using (ManagementBaseObject outParams = process.InvokeMethod("Create", inParams, null))
{
int retVal = Convert.ToInt32(outParams.Properties["ReturnValue"].Value);

return (retVal == 0);
}
}
}



}
else
{
return true;//if exists, return true; you may want to return false, of course
}
}
return false;
}

No comments:

Post a Comment