protected override void ProcessRecord() { // テスト自動生成 TestGenerator.RegistryKey(Path); using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, true, true)) { } }
/// <summary> /// RegistrySummaryリストを取得 /// </summary> /// <param name="path">レジストリキーのパス</param> /// <param name="ignoreSecurity">セキュリティ情報を除外して比較</param> /// <param name="ignoreValues">レジストリ値を場外して比較</param> /// <returns></returns> private List <RegistrySummary> GetSummaryList(string path, bool ignoreSecurity, bool ignoreValues) { int startLength = 0; List <RegistrySummary> summaryList = new List <RegistrySummary>(); Action <RegistryKey> getSummary = null; getSummary = (targetPath) => { RegistrySummary summary = new RegistrySummary(targetPath, startLength, ignoreSecurity, ignoreValues); summary.Name = ""; summaryList.Add(summary); //summaryList.Add(new RegistrySummary(targetPath, startLength, ignoreSecurity, ignoreValues)); foreach (string keyName in targetPath.GetSubKeyNames()) { using (RegistryKey subTargetKey = targetPath.OpenSubKey(keyName, false)) { getSummary(subTargetKey); } } }; using (RegistryKey startKey = RegistryControl.GetRegistryKey(path, false, false)) { startLength = startKey.Name.Length; getSummary(startKey); } return(summaryList); }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, true)) { if (Name == null) { try { // テスト自動生成 _generator.RegistryPath(RegistryPath); regKey.DeleteSubKeyTree(""); } catch { using (Process proc = new Process()) { proc.StartInfo.FileName = "reg"; proc.StartInfo.Arguments = $"delete \"{RegistryPath}\" /f"; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } } else { // テスト自動生成 _generator.RegistryName(RegistryPath, Name); regKey.DeleteValue(Name); } } }
// レジストリ値をコピー private void CopyRegistryValue(string source, string destination, string name, string destinationName) { using (RegistryKey sourceKey = RegistryControl.GetRegistryKey(source, false, true)) using (RegistryKey destinationKey = RegistryControl.GetRegistryKey(destination, true, true)) { RegistryValueKind valueKind = sourceKey.GetValueKind(name); object sourceValue = valueKind == RegistryValueKind.ExpandString ? sourceKey.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames) : sourceKey.GetValue(name); if (destinationName == null) { destinationName = name; } // テスト自動生成 _generator.RegistryName(source, name); _generator.RegistryName(source, destinationName); _generator.RegistryValue(source, destinationName, RegistryControl.RegistryValueToString(sourceKey, name, valueKind, true)); destinationKey.SetValue(destinationName, sourceValue, valueKind); // コピー元を削除する場合 sourceKey.DeleteValue(name); } }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { SearchKeyNameValue(regKey); } switch (DataType) { case Item.XML: WriteObject( DataSerializer.Serialize <List <RegistryKeyNameValue> >(KNVList, PSFile.Serialize.DataType.Xml)); break; case Item.JSON: WriteObject( DataSerializer.Serialize <List <RegistryKeyNameValue> >(KNVList, PSFile.Serialize.DataType.Json)); break; case Item.YML: WriteObject( DataSerializer.Serialize <List <RegistryKeyNameValue> >(KNVList, PSFile.Serialize.DataType.Yml)); break; case Item.TXT: break; default: WriteObject(KNVList); break; } }
protected override void ProcessRecord() { if (Name == null) { // レジストリキーの取得 if (ReturnReadonlyKey) { WriteObject(RegistryControl.GetRegistryKey(RegistryPath, false, false)); } else if (ReturnWritableKey) { WriteObject(RegistryControl.GetRegistryKey(RegistryPath, true, true)); } else { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { WriteObject(new RegistrySummary(regKey, IgnoreSecurity, IgnoreValues), true); } } } else { // レジストリ値の取得 using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { RegistryValueKind valueKind = regKey.GetValueKind(Name); if (RawValue) { switch (valueKind) { case RegistryValueKind.ExpandString: WriteObject(NoResolv ? regKey.GetValue(Name, "", RegistryValueOptions.DoNotExpandEnvironmentNames) : regKey.GetValue(Name)); break; case RegistryValueKind.None: WriteObject(null); break; default: WriteObject(regKey.GetValue(Name)); break; } } else { WriteObject(RegistryControl.RegistryValueToString(regKey, Name, valueKind, NoResolv)); } } } }
protected override void ProcessRecord() { bool isChange = false; using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, true)) { RegistrySecurity security = regKey.GetAccessControl(); AuthorizationRuleCollection rules = security.GetAccessRules(true, false, typeof(NTAccount)); if (All) { // テスト自動生成 TestGenerator.RegistryAccess(Path, "", false); foreach (RegistryAccessRule rule in rules) { security.RemoveAccessRule(rule); isChange = true; } } else { foreach (RegistryAccessRule rule in rules) { if (Account.Contains("\\") && rule.IdentityReference.Value.Equals(Account, StringComparison.OrdinalIgnoreCase)) { // テスト自動生成 TestGenerator.RegistryAccess(Path, RegistryControl.AccessRuleToString(rule), true); security.RemoveAccessRule(rule); isChange = true; } else if (!Account.Contains("\\") && rule.IdentityReference.Value.EndsWith("\\" + Account, StringComparison.OrdinalIgnoreCase)) { // テスト自動生成 TestGenerator.RegistryAccess(Path, RegistryControl.AccessRuleToString(rule), true); security.RemoveAccessRule(rule); isChange = true; } } } if (isChange) { regKey.SetAccessControl(security); } } WriteObject(new RegistryKeyInfo(Path, true)); }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { // レジストリキーの有無チェック if (regKey == null) { Console.Error.WriteLine("対象のレジストリキー (Path) 無し: {0}", RegistryPath.ToString()); return; } if (Target == Item.PATH) { retValue = true; return; } // レジストリのパラメータ名/種類/値のチェック if (Target == Item.NAME || Target == Item.TYPE || Target == Item.VALUE) { CheckRegValue(regKey); return; } // 所有者チェック if (Target == Item.OWNER) { CheckOwner(regKey); return; } // アクセス権チェック if (Target == Item.ACCESS) { CheckAccess(regKey); return; } // Accountチェック if (Target == Item.ACCOUNT) { CheckAccount(regKey); return; } // Inheritedチェック if (Target == Item.INHERITED) { CheckInherited(regKey); return; } } }
protected override void ProcessRecord() { // 管理者実行確認 Functions.CheckAdmin(); using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { if (regKey != null) { return; } } // テスト自動生成 _generator.RegistryPath(RegistryPath); string keyName = RegistryPath.Substring(RegistryPath.IndexOf("\\") + 1); RegistryHive.Load(keyName, DatFile); // ロード成功確認 using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { if (regKey != null) { WriteObject(new RegistrySummary(regKey)); return; } } // ロード失敗時の再ロード用コマンド using (Process proc = new Process()) { proc.StartInfo.FileName = "reg.exe"; proc.StartInfo.Arguments = $"load \"{RegistryPath}\" \"{DatFile}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { if (regKey != null) { WriteObject(new RegistrySummary(regKey)); } } }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, true)) { bool isChange = false; RegistrySecurity security = security = regKey.GetAccessControl(); if (All) { // テスト自動生成 _generator.RegistryAccess(RegistryPath, "", false); foreach (RegistryAccessRule rule in security.GetAccessRules(true, false, typeof(NTAccount))) { security.RemoveAccessRule(rule); isChange = true; } } else { foreach (RegistryAccessRule rule in security.GetAccessRules(true, false, typeof(NTAccount))) { string account = rule.IdentityReference.Value; // テスト自動生成 _generator.RegistryAccount(RegistryPath, account); if (Account.Contains("\\") && account.Equals(Account, StringComparison.OrdinalIgnoreCase) || !Account.Contains("\\") && account.EndsWith("\\" + Account, StringComparison.OrdinalIgnoreCase)) { security.RemoveAccessRule(rule); isChange = true; } } } if (isChange) { regKey.SetAccessControl(security); } WriteObject(new RegistrySummary(regKey, true)); } }
/// <summary> /// RegistrySummaryのリストを取得 /// </summary> /// <returns>RegistrySummaryのList</returns> private List <RegistrySummary> GetPRegList() { List <RegistrySummary> pregList = new List <RegistrySummary>(); Action <RegistryKey> getPReg = null; getPReg = (targetKey) => { pregList.Add(new RegistrySummary(targetKey, true)); foreach (string keyName in targetKey.GetSubKeyNames()) { using (RegistryKey subTargetKey = targetKey.OpenSubKey(keyName, false)) { getPReg(subTargetKey); } } }; using (RegistryKey startKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { getPReg(startKey); } return(pregList); }
protected override void ProcessRecord() { // テスト自動生成 TestGenerator.RegistryKey(Path); // 管理者実行確認 Message.CheckAdmin(); using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, false)) { if (regKey == null) { return; } } string keyName = Path.Substring(Path.IndexOf("\\") + 1); RegistryHive.UnLoad(keyName); // アンロード成功確認 using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, false)) { if (regKey == null) { return; } } // アンロード失敗時の再アンロード用コマンド using (Process proc = new Process()) { proc.StartInfo.FileName = "reg.exe"; proc.StartInfo.Arguments = $"unload \"{Path}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } }
/// <summary> /// RegistryKeyInfoリストを取得 /// </summary> /// <param name="path">レジストリキーのパス</param> /// <param name="ignoreSecurity">セキュリティ情報を除外して比較</param> /// <param name="ignoreValues">レジストリ値を場外して比較</param> /// <returns></returns> private List <RegistryKeyInfo> GetPRegList(string path, bool ignoreSecurity, bool ignoreValues) { int startKeyLength = 0; List <RegistryKeyInfo> pregList = new List <RegistryKeyInfo>(); Action <RegistryKey> getPReg = null; getPReg = (targetKey) => { pregList.Add(new RegistryKeyInfo(targetKey, startKeyLength, ignoreSecurity, ignoreValues)); foreach (string keyName in targetKey.GetSubKeyNames()) { using (RegistryKey subTargetKey = targetKey.OpenSubKey(keyName, false)) { getPReg(subTargetKey); } } }; using (RegistryKey startKey = RegistryControl.GetRegistryKey(path, false, false)) { startKeyLength = startKey.Name.Length; getPReg(startKey); } return(pregList); }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, true, true)) { if (regKey == null) { return; } RegistrySecurity security = null; // Access文字列からの設定 // ""で全アクセス権設定を削除 if (Access != null) { if (security == null) { security = regKey.GetAccessControl(); } foreach (RegistryAccessRule removeRule in security.GetAccessRules(true, false, typeof(NTAccount))) { security.RemoveAccessRule(removeRule); } // テスト自動生成 _generator.RegistryAccess(RegistryPath, Access, false); if (Access != string.Empty) // このif文分岐が無くても同じ挙動するけれど、一応記述 { foreach (RegistryAccessRule addRule in RegistryControl.StringToAccessRules(Access)) { security.AddAccessRule(addRule); } } } // 上位からのアクセス権継承の設定変更 if (Inherited != Item.NONE) { if (security == null) { security = regKey.GetAccessControl(); } // テスト自動生成 _generator.RegistryInherited(RegistryPath, 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) { regKey.SetAccessControl(security); } } // 所有者変更 if (Owner != null) { string subinacl = EmbeddedResource.GetSubinacl(Item.APPLICATION_NAME); // 管理者実行確認 Functions.CheckAdmin(); // テスト自動生成 _generator.RegistryOwner(RegistryPath, Owner); using (Process proc = new Process()) { proc.StartInfo.FileName = subinacl; proc.StartInfo.Arguments = $"/subkeyreg \"{RegistryPath}\" /owner=\"{Owner}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } // レジストリ値の設定 if (Name != null) { // テスト自動生成 _generator.RegistryType(RegistryPath, Name, Type); _generator.RegistryValue(RegistryPath, Name, Value); switch (Type) { case Item.REG_SZ: Registry.SetValue(RegistryPath, Name, Value, RegistryValueKind.String); break; case Item.REG_BINARY: Registry.SetValue(RegistryPath, Name, RegistryControl.StringToRegBinary(Value), RegistryValueKind.Binary); break; case Item.REG_DWORD: Registry.SetValue(RegistryPath, Name, int.Parse(Value), RegistryValueKind.DWord); break; case Item.REG_QWORD: Registry.SetValue(RegistryPath, Name, long.Parse(Value), RegistryValueKind.QWord); break; case Item.REG_MULTI_SZ: Registry.SetValue(RegistryPath, Name, Functions.SplitBQt0(Value), RegistryValueKind.MultiString); break; case Item.REG_EXPAND_SZ: Registry.SetValue(RegistryPath, Name, Value, RegistryValueKind.ExpandString); break; case Item.REG_NONE: Registry.SetValue(RegistryPath, Name, new byte[2] { 0, 0 }, RegistryValueKind.None); break; } } /* 実行していて結構うっとおしいので、出力しないことにします。 * using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, false)) * { * WriteObject(new RegistrySummary(regKey, true)); * } */ }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, true)) { if (regKey == null) { return; } RegistrySecurity security = null; // Account, Rights, AccessControlから追加 if (!string.IsNullOrEmpty(Account)) { if (security == null) { security = regKey.GetAccessControl(); } string accessString = string.Format("{0};{1};{2};{3};{4}", Account, _Rights, Recursive ? Item.CONTAINERINHERIT + ", " + Item.OBJECTINHERIT : Item.NONE, Item.NONE, AccessControl); // テスト自動生成 _generator.RegistryAccess(RegistryPath, accessString, true); foreach (RegistryAccessRule addRule in RegistryControl.StringToAccessRules(accessString)) { security.AddAccessRule(addRule); } } // Access文字列からの設定 if (!string.IsNullOrEmpty(Access)) { if (security == null) { security = regKey.GetAccessControl(); } // テスト自動生成 _generator.RegistryAccess(RegistryPath, Access, true); foreach (RegistryAccessRule rule in RegistryControl.StringToAccessRules(Access)) { security.AddAccessRule(rule); } } // 上位からのアクセス権継承の設定変更 if (Inherited != Item.NONE) { if (security == null) { security = regKey.GetAccessControl(); } // テスト自動生成 _generator.RegistryInherited(RegistryPath, 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) { regKey.SetAccessControl(security); } WriteObject(new RegistrySummary(regKey, true)); } }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { if (regKey != null) { return; } } // テスト自動生成 _generator.RegistryPath(RegistryPath); using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, true, true)) { RegistrySecurity security = null; // Access文字列からの設定 if (!string.IsNullOrEmpty(Access)) { if (security == null) { security = regKey.GetAccessControl(); } // テスト自動生成 _generator.RegistryAccess(RegistryPath, Access, false); foreach (RegistryAccessRule rule in RegistryControl.StringToAccessRules(Access)) { security.AddAccessRule(rule); } } // 上位からのアクセス権継承の設定変更 if (Inherited != Item.NONE) { if (security == null) { security = regKey.GetAccessControl(); } // テスト自動生成 _generator.RegistryInherited(RegistryPath, 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) { regKey.SetAccessControl(security); } } // 所有者変更 if (Owner != null) { // 埋め込みのsubinacl.exeを展開 string subinacl = EmbeddedResource.GetSubinacl(Item.APPLICATION_NAME); // 管理者実行確認 Functions.CheckAdmin(); // テスト自動生成 _generator.RegistryOwner(RegistryPath, Owner); using (Process proc = new Process()) { proc.StartInfo.FileName = subinacl; proc.StartInfo.Arguments = $"/subkeyreg \"{RegistryPath}\" /owner=\"{Owner}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { WriteObject(new RegistrySummary(regKey, true)); } }
// レジストリキーをコピー private void CopyRegistryKey(string source, string destination) { Action <RegistryKey, RegistryKey> copyRegKey = null; copyRegKey = (srcKey, dstKey) => { foreach (string paramName in srcKey.GetValueNames()) { RegistryValueKind valueKind = srcKey.GetValueKind(paramName); dstKey.SetValue( paramName, valueKind == RegistryValueKind.ExpandString ? srcKey.GetValue(paramName, "", RegistryValueOptions.DoNotExpandEnvironmentNames) : srcKey.GetValue(paramName), valueKind); } foreach (string keyName in srcKey.GetSubKeyNames()) { using (RegistryKey subSrcKey = srcKey.OpenSubKey(keyName, false)) using (RegistryKey subDstKey = dstKey.CreateSubKey(keyName, true)) { try { copyRegKey(subSrcKey, subDstKey); } catch (System.Security.SecurityException) { Console.WriteLine("アクセス拒否:SecurityException\r\n" + keyName); } catch (UnauthorizedAccessException) { Console.WriteLine("アクセス拒否:UnauthorizedAccessException\r\n" + keyName); } catch (ArgumentException) { // 無効なValueKindのレジストリ値への対策 // reg copyコマンドでコピー実行 using (Process proc = new Process()) { proc.StartInfo.FileName = "reg.exe"; proc.StartInfo.Arguments = $@"copy ""{subSrcKey.ToString()}"" ""{subDstKey.ToString()}"" /s /f"; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } } } }; using (RegistryKey sourceKey = RegistryControl.GetRegistryKey(source, false, true)) using (RegistryKey destinationKey = RegistryControl.GetRegistryKey(destination, true, true)) { // テスト自動生成 _generator.RegistryPath(source); _generator.RegistryPath(destination); _generator.RegistryCompare(source, destination, true, false); copyRegKey(sourceKey, destinationKey); // コピー元を削除する場合 sourceKey.DeleteSubKeyTree(""); } }
protected override void ProcessRecord() { using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, false)) { // レジストリキーの有無チェック if (regKey == null) { // 全条件でキーの有無チェック Console.Error.WriteLine("対象のレジストリキー (Path) 無し: {0}", Path.ToString()); return; } if (Target == Item.KEY) { retValue = true; return; } // レジストリのパラメータ名/種類/値のチェック (長かったので別のメソッドに) if (Target == Item.NAME || Target == Item.TYPE || Target == Item.VALUE) { CheckRegValue(regKey); return; } // 所有者チェック RegistrySecurity security = regKey.GetAccessControl(); if (Target == Item.OWNER) { string owner = security.GetOwner(typeof(NTAccount)).Value; retValue = owner == Owner; if (!retValue) { Console.Error.WriteLine("所有者名不一致: {0} / {1}", Owner, owner); } return; } // アクセス権チェック if (Target == Item.ACCESS) { if (TestMode == Item.CONTAIN) { // Accessパラメータで指定したAccess文字列が、対象のレジストリキーに含まれているかチェック // Access文字列は複数の場合は、全て対象のレジストリキーに含まれているかをチェック string tempAccess = new RegistryKeyInfo(regKey, false, true).Access; //string tempAccess = RegistryControl.AccessToString(regKey); string[] tempAccessArray = tempAccess.Contains("/") ? tempAccess.Split('/') : new string[1] { tempAccess }; foreach (string ruleString in Access.Contains("/") ? Access.Split('/') : new string[1] { Access }) { retValue = tempAccessArray.Any(x => x.Equals(ruleString, StringComparison.OrdinalIgnoreCase)); if (!retValue) { Console.Error.WriteLine("指定のアクセス権無し: {0} / {1}", Access, tempAccess); break; } } } else { string tempAccess = new RegistryKeyInfo(regKey, false, true).Access; //string access = RegistryControl.AccessToString(regKey); retValue = tempAccess == Access; if (!retValue) { Console.Error.WriteLine("アクセス権不一致: {0} / {1}", Access, tempAccess); } } return; } // 継承設定チェック if (Target == Item.INHERIT) { //retValue = !security.AreAccessRulesProtected == IsInherit; bool tempInherit = new RegistryKeyInfo(regKey, false, true).Inherited; retValue = tempInherit == IsInherit; if (!retValue) { Console.Error.WriteLine("継承設定不一致: {0} / {1}", IsInherit, tempInherit); } return; } } }
protected override void ProcessRecord() { if (Name == null) { // レジストリキーの取得 WriteObject(new RegistryKeyInfo(Path, true)); } else { // レジストリ値の取得 try { using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, false)) { RegistryValueKind valueKind = regKey.GetValueKind(Name); if (RawValue) { switch (valueKind) { case RegistryValueKind.ExpandString: WriteObject(NoResolv ? regKey.GetValue(Name, "", RegistryValueOptions.DoNotExpandEnvironmentNames) : regKey.GetValue(Name)); break; case RegistryValueKind.None: WriteObject(null); break; default: WriteObject(regKey.GetValue(Name)); break; } } else { WriteObject(RegistryControl.RegistryValueToString(regKey, Name, valueKind, NoResolv)); /* * switch (valueKind) * { * case RegistryValueKind.String: * case RegistryValueKind.DWord: * case RegistryValueKind.QWord: * WriteObject(regKey.GetValue(Name)); * break; * case RegistryValueKind.ExpandString: * WriteObject(NoResolv ? * regKey.GetValue(Name, "", RegistryValueOptions.DoNotExpandEnvironmentNames) : * regKey.GetValue(Name)); * break; * case RegistryValueKind.Binary: * //WriteObject(RegistryControl.RegBinaryBytesToString(regKey.GetValue(Name) as byte[])); * WriteObject(RegistryControl.RegistryValueToString(regKey, Name, valueKind, true)); * break; * case RegistryValueKind.MultiString: * WriteObject( * string.Join("\\0", regKey.GetValue(Name) as string[])); * break; * case RegistryValueKind.None: * //WriteObject(regKey.GetValue(Name)); * WriteObject(null); * break; * } */ } } } catch (Exception e) { TextWriter errorWriter = Console.Error; errorWriter.WriteLine(e.Message); } } }
protected override void ProcessRecord() { bool isChange = false; using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, true)) { if (regKey == null) { return; } RegistrySecurity security = regKey.GetAccessControl(); // アクセス権設定 if (!string.IsNullOrEmpty(Account)) { RegistryAccessRule rule = new RegistryAccessRule( new NTAccount(Account), (RegistryRights)Enum.Parse(typeof(RegistryRights), _Rights), Recursive ? InheritanceFlags.ContainerInherit : InheritanceFlags.None, PropagationFlags.None, (AccessControlType)Enum.Parse(typeof(AccessControlType), AccessControl)); // テスト自動生成 TestGenerator.RegistryAccess(Path, RegistryControl.AccessRuleToString(rule), true); security.SetAccessRule(rule); isChange = true; } // Access文字列からの設定 if (!string.IsNullOrEmpty(Access)) { /* * foreach (RegistryAccessRule accessRule in RegistryControl.StringToAccess(Access)) * { * security.SetAccessRule(accessRule); * isChange = true; * } */ foreach (string ruleString in Access.Contains("/") ? Access.Split('/') : new string[1] { Access }) { // テスト自動生成 TestGenerator.RegistryAccess(Path, ruleString, true); security.SetAccessRule(RegistryControl.StringToAccessRule(ruleString)); isChange = true; } } // 上位からのアクセス権継承の設定変更 switch (Inherit) { case Item.ENABLE: TestGenerator.RegistryInherit(Path, true); security.SetAccessRuleProtection(false, false); isChange = true; break; case Item.DISABLE: TestGenerator.RegistryInherit(Path, false); security.SetAccessRuleProtection(true, true); isChange = true; break; case Item.REMOVE: TestGenerator.RegistryInherit(Path, false); security.SetAccessRuleProtection(true, false); isChange = true; break; } if (isChange) { regKey.SetAccessControl(security); } } WriteObject(new RegistryKeyInfo(Path, true)); }
protected override void ProcessRecord() { List <string> commandList = new List <string>(); if (Name == null) { Action <RegistryKey> measureRegistry = null; measureRegistry = (targetKey) => { List <string> valueNameList = new List <string>(targetKey.GetValueNames()); valueNameList.Sort(); if (valueNameList.Count > 0) { valueNameList.ForEach(x => commandList.Add(Dos ? CreateDosCommand(targetKey, x) : CreateSetCommand(targetKey, x))); } else { // レジストリ値設定無し。空レジストリキー作成 if (Dos) { commandList.Add(string.Format("reg add \"{0}\" /ve /f", ReplaceDoller(targetKey.ToString()))); commandList.Add(string.Format("reg delete \"{0}\" /ve /f", ReplaceDoller(targetKey.ToString()))); } else { commandList.Add(string.Format("New-Registry -Path \"{0}\"", ReplaceDoller(targetKey.ToString()))); } } if (Recursive) { foreach (string keyName in targetKey.GetSubKeyNames()) { using (RegistryKey subTargetKey = targetKey.OpenSubKey(keyName, false)) { measureRegistry(subTargetKey); } } } }; using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { measureRegistry(regKey); } } else { using (RegistryKey regKey = RegistryControl.GetRegistryKey(RegistryPath, false, false)) { commandList.Add(Dos ? CreateDosCommand(regKey, Name) : CreateSetCommand(regKey, Name)); } } // コンソール/ファイルへ出力 if (OutputFile == null) { WriteObject(commandList); } else { Environment.CurrentDirectory = this.SessionState.Path.CurrentFileSystemLocation.Path; using (StreamWriter sw = new StreamWriter(OutputFile, false, Encoding.GetEncoding("Shift_JIS"))) { sw.WriteLine(string.Join("\r\n", commandList)); } } }
protected override void ProcessRecord() { if (Inherit != Item.NONE || !string.IsNullOrEmpty(Access)) { using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, true, true)) { RegistrySecurity security = regKey.GetAccessControl(); // 上位からのアクセス権継承の設定変更 switch (Inherit) { case Item.ENABLE: TestGenerator.RegistryInherit(Path, true); security.SetAccessRuleProtection(false, false); break; case Item.DISABLE: TestGenerator.RegistryInherit(Path, false); security.SetAccessRuleProtection(true, true); break; case Item.REMOVE: TestGenerator.RegistryInherit(Path, false); security.SetAccessRuleProtection(true, false); break; } // Access文字列からのアクセス権設定 if (!string.IsNullOrEmpty(Access)) { foreach (string ruleString in Access.Contains("/") ? Access.Split('/') : new string[1] { Access }) { // テスト自動生成 TestGenerator.RegistryAccess(Path, ruleString, true); security.SetAccessRule(RegistryControl.StringToAccessRule(ruleString)); } } /* * foreach (RegistryAccessRule accessRule in RegistryControl.StringToAccess(Access)) * { * security.SetAccessRule(accessRule); * } */ regKey.SetAccessControl(security); } } // 所有者変更 if (Owner != null) { // テスト自動生成 TestGenerator.RegistryOwner(Path, Owner); // 埋め込みのsubinacl.exeを展開 string tempDir = System.IO.Path.Combine( Environment.ExpandEnvironmentVariables("%TEMP%"), "PowerReg"); string subinacl = System.IO.Path.Combine(tempDir, "subinacl.exe"); if (!File.Exists(subinacl)) { EmbeddedResource.Expand(tempDir); } // 管理者実行確認 Message.CheckAdmin(); using (Process proc = new Process()) { proc.StartInfo.FileName = subinacl; proc.StartInfo.Arguments = $"/subkeyreg \"{Path}\" /owner=\"{Owner}\""; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); } } // レジストリ値の設定 if (Name != null) { // テスト自動生成 TestGenerator.RegistryName(Path, Name); TestGenerator.RegistryType(Path, Name, Type); TestGenerator.RegistryValue(Path, Name, Value); switch (Type) { case Item.REG_SZ: Registry.SetValue(Path, Name, Value, RegistryValueKind.String); break; case Item.REG_BINARY: Registry.SetValue(Path, Name, RegistryControl.RegBinaryStringToBytes(Value), RegistryValueKind.Binary); break; case Item.REG_DWORD: Registry.SetValue(Path, Name, int.Parse(Value), RegistryValueKind.DWord); break; case Item.REG_QWORD: Registry.SetValue(Path, Name, long.Parse(Value), RegistryValueKind.QWord); break; case Item.REG_MULTI_SZ: Registry.SetValue(Path, Name, Regex.Split(Value, "\\\\0"), RegistryValueKind.MultiString); break; case Item.REG_EXPAND_SZ: Registry.SetValue(Path, Name, Value, RegistryValueKind.ExpandString); break; case Item.REG_NONE: Registry.SetValue(Path, Name, new byte[2] { 0, 0 }, RegistryValueKind.None); break; } } }