1

我在 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();
        }
    }
4

1 回答 1

0

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

在保存文件夹的服务器上打开 RemotePowershell。然后从这里使用 Cmdlet

一些代码片段:

打开运行空间:

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
        }
    }
}
于 2018-03-14T09:41:35.783 回答