目录级别远程共享文件夹上的配额

时间:2012-07-13 08:09:42

标签: directory shared ntfs quota

AD中有2台服务器(2008R2) 在其中一个我有共享文件夹(c:\ Shared \ dirForUserAAA ==> \ DC1 \ dir1) 在另一个我有c#程序,必须管理\ DC1 \ dir1上的文件夹配额 是否有可能以及如何做到这一点?

我尝试使用这段代码,但它只适用于本地路径:(

       public static void SetQuotaToFolder(string UNCPathForQuota, int quotaLimitBytes)
        {
            if (!Directory.Exists(UNCPathForQuota))
            {
                Directory.CreateDirectory(UNCPathForQuota);
            }


            // Create our interface
            IFsrmQuotaManager FSRMQuotaManager = new FsrmQuotaManagerClass();
            IFsrmQuota Quota = null;

            try
            {
                // First we need to see if there is already a quota on the directory.
                Quota = FSRMQuotaManager.GetQuota(UNCPathForQuota);
                // If there is quota then we just set it to our new size
                Quota.QuotaLimit = quotaLimitBytes;
            }
            catch (COMException e)
            {
                unchecked
                {
                    if (e.ErrorCode == (int)0x80045301)
                    {
                        // There is no quota on this directory so we need to create it.
                        Quota = FSRMQuotaManager.CreateQuota(UNCPathForQuota);
                        // And then set our desired quota
                        Quota.QuotaLimit = quotaLimitBytes;
                    }
                    else
                    {
                        // some other COM exception occured so we return the error
                        Console.WriteLine(e);
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                // Generic error handling would go here
                Console.WriteLine(e);
                return;
            }

            // and finally we commit our changes.
            Quota.Commit();
        }
    }

1 个答案:

答案 0 :(得分:0)

旧问题,但如果有人需要提示:

在保存文件夹的服务器上打开RemotePowershell。然后使用here

中的Cmdlet

一些代码段:

打开Runspace:

public static Runspace CreateAndOpen(string domain, string username, string password, string computername)
{
    string userName = username + "@" + domain;
    var securePassword = password.ToSecureString();

    PSCredential credential = new PSCredential(username, securePassword);
    var connectionInfo = new WSManConnectionInfo(false, computername, 5985, "/wsman", shellUri, credential);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
    connectionInfo.OpenTimeout = 2 * 60 * 1000; // 2 minutes
    Runspace powershellRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
    powershellRunspace.Open();

    return powershellRunspace;
}

在路径上设置配额

public void SetQuotaTemplateOnPath(Runspace runspace, string path, string template)
{
    using (var pipe = runspace.CreatePipeline())
    {
        var newFsrmQuotaCommand = new Command("New-FsrmQuota");
        newFsrmQuotaCommand.Parameters.Add("Path", path);
        newFsrmQuotaCommand.Parameters.Add("Template", template);
        newFsrmQuotaCommand.Parameters.Add("Confirm", false);
        pipe.Commands.Add(newFsrmQuotaCommand);

        var results = pipe.Invoke();

        if (pipe.Error.Count > 0)
        {
             //Handle error
        }
    }
}