protected override void ProcessRecord() { if (Directory.Exists(DirectoryPath)) { DirectorySecurity security = null; // Account, Rights, AccessControlから追加 if (!string.IsNullOrEmpty(Account)) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } string accessString = string.Format("{0};{1};{2};{3};{4}", Account, _Rights, Recursive ? Item.CONTAINERINHERIT + ", " + Item.OBJECTINHERIT : Item.NONE, Item.NONE, AccessControl); // テスト自動生成 _generator.DirectoryAccess(DirectoryPath, accessString, true); foreach (FileSystemAccessRule addRule in DirectoryControl.StringToAccessRules(accessString)) { security.AddAccessRule(addRule); } } // Access文字列で追加 if (!string.IsNullOrEmpty(Access)) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryAccess(DirectoryPath, Access, true); foreach (FileSystemAccessRule addRule in DirectoryControl.StringToAccessRules(Access)) { security.AddAccessRule(addRule); } } // Inherited設定 if (Inherited != Item.NONE) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryInherited(DirectoryPath, Inherited == Item.ENABLE); switch (Inherited) { case Item.ENABLE: security.SetAccessRuleProtection(false, false); break; case Item.DISABLE: security.SetAccessRuleProtection(true, true); break; case Item.REMOVE: security.SetAccessRuleProtection(true, false); break; } } if (security != null) { Directory.SetAccessControl(DirectoryPath, security); } // フォルダー属性を追加 if (!string.IsNullOrEmpty(_Attributes)) { // テスト自動生成 _generator.DirectoryAttributes(DirectoryPath, _Attributes, true); FileAttributes nowAttr = File.GetAttributes(DirectoryPath); FileAttributes addAttr = (FileAttributes)Enum.Parse(typeof(FileAttributes), _Attributes); File.SetAttributes(DirectoryPath, nowAttr | addAttr); } WriteObject(new DirectorySummary(DirectoryPath, true)); } }
protected override void ProcessRecord() { if (Directory.Exists(DirectoryPath)) { return; } // テスト自動生成 _generator.DirectoryPath(DirectoryPath); Directory.CreateDirectory(DirectoryPath); DirectorySecurity security = null; // Access設定 if (!string.IsNullOrEmpty(Access)) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryAccess(DirectoryPath, Access, false); foreach (FileSystemAccessRule addRule in DirectoryControl.StringToAccessRules(Access)) { security.AddAccessRule(addRule); } } // Owner設定 if (!string.IsNullOrEmpty(Owner)) { // 埋め込みのsubinacl.exeを展開 string subinacl = EmbeddedResource.GetSubinacl(Item.APPLICATION_NAME); // 管理者実行確認 Functions.CheckAdmin(); // テスト自動生成 _generator.DirectoryOwner(DirectoryPath, Owner); using (Process proc = new Process()) { proc.StartInfo.FileName = subinacl; proc.StartInfo.Arguments = $"/subdirectories \"{DirectoryPath}\" /setowner=\"{Owner}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } // Inherited設定 if (Inherited != Item.NONE) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryInherited(DirectoryPath, Inherited == Item.ENABLE); switch (Inherited) { case Item.ENABLE: security.SetAccessRuleProtection(false, false); break; case Item.DISABLE: security.SetAccessRuleProtection(true, true); break; case Item.REMOVE: security.SetAccessRuleProtection(true, false); break; } } if (security != null) { Directory.SetAccessControl(DirectoryPath, security); } // 作成日時 if (CreationTime != null) { // テスト自動生成 _generator.DirectoryCreationTime(DirectoryPath, (DateTime)CreationTime); Directory.SetCreationTime(DirectoryPath, (DateTime)CreationTime); } // 更新一時 if (LastWriteTime != null) { // テスト自動生成 _generator.DirectoryLastWriteTime(DirectoryPath, (DateTime)LastWriteTime); Directory.SetLastWriteTime(DirectoryPath, (DateTime)LastWriteTime); } // フォルダー属性 if (!string.IsNullOrEmpty(_Attributes)) { // テスト自動生成 _generator.DirectoryAttributes(DirectoryPath, _Attributes, false); if (!_Attributes.Contains(Item.DIRECTORY)) { _Attributes += ", " + Item.DIRECTORY; } File.SetAttributes(DirectoryPath, (FileAttributes)Enum.Parse(typeof(FileAttributes), _Attributes)); } WriteObject(new DirectorySummary(DirectoryPath, true)); }
protected override void ProcessRecord() { if (Directory.Exists(DirectoryPath)) { DirectorySecurity security = null; // Access設定 // ""で全アクセス権設定を削除 if (Access != null) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryAccess(DirectoryPath, Access, false); foreach (FileSystemAccessRule removeRule in security.GetAccessRules(true, false, typeof(NTAccount))) { security.RemoveAccessRule(removeRule); } if (Access != string.Empty) // このif文分岐が無くても同じ挙動するけれど、一応記述 { foreach (FileSystemAccessRule addRule in DirectoryControl.StringToAccessRules(Access)) { security.AddAccessRule(addRule); } } } // Owner設定 if (!string.IsNullOrEmpty(Owner)) { // 埋め込みのsubinacl.exeを展開 string subinacl = EmbeddedResource.GetSubinacl(Item.APPLICATION_NAME); // 管理者実行確認 Functions.CheckAdmin(); // テスト自動生成 _generator.DirectoryOwner(DirectoryPath, Owner); using (Process proc = new Process()) { proc.StartInfo.FileName = subinacl; proc.StartInfo.Arguments = $"/subdirectories \"{DirectoryPath}\" /setowner=\"{Owner}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } // Inherited設定 if (Inherited != Item.NONE) { if (security == null) { security = Directory.GetAccessControl(DirectoryPath); } // テスト自動生成 _generator.DirectoryInherited(DirectoryPath, Inherited == Item.ENABLE); switch (Inherited) { case Item.ENABLE: security.SetAccessRuleProtection(false, false); break; case Item.DISABLE: security.SetAccessRuleProtection(true, true); break; case Item.REMOVE: security.SetAccessRuleProtection(true, false); break; } } if (security != null) { Directory.SetAccessControl(DirectoryPath, security); } // 作成日時 if (CreationTime != null) { // テスト自動生成 _generator.DirectoryCreationTime(DirectoryPath, (DateTime)CreationTime); Directory.SetCreationTime(DirectoryPath, (DateTime)CreationTime); } // 更新一時 if (LastWriteTime != null) { // テスト自動生成 _generator.DirectoryLastWriteTime(DirectoryPath, (DateTime)LastWriteTime); Directory.SetLastWriteTime(DirectoryPath, (DateTime)LastWriteTime); } // フォルダー属性 if (!string.IsNullOrEmpty(_Attributes)) { // テスト自動生成 _generator.DirectoryAttributes(DirectoryPath, _Attributes, false); if (!_Attributes.Contains(Item.DIRECTORY)) { _Attributes += ", " + Item.DIRECTORY; } File.SetAttributes(DirectoryPath, (FileAttributes)Enum.Parse(typeof(FileAttributes), _Attributes)); } // セキュリティブロックの解除 if (RemoveSecurityBlock) { foreach (string fileName in Directory.GetFiles(DirectoryPath, "*", SearchOption.AllDirectories)) { // テスト自動生成 _generator.FileSecurityBlock(fileName, false); FileControl.RemoveSecurityBlock(fileName); } } /* 実行していて結構うっとおしいので、出力しないことにします。 * WriteObject(new DirectorySummary(Path, true)); */ } }