-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated ActiveDirectoryListUsersOfGroup to allow for TAB exporting
- Loading branch information
1 parent
b5c6e9c
commit 1c11bec
Showing
6 changed files
with
193 additions
and
80 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright (c) 2021 Steven Foster ([email protected]) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Linq; | ||
using HavokMultimedia.Utilities.Console.External; | ||
|
||
namespace HavokMultimedia.Utilities.Console.Commands | ||
{ | ||
public abstract class ActiveDirectoryListBase : ActiveDirectoryBase | ||
{ | ||
protected enum Format { Display, TAB } | ||
protected abstract string Summary { get; } | ||
protected virtual string Example => "-h=192.168.1.5 -u=administrator -p=testpass"; | ||
protected override void CreateHelp(CommandHelpBuilder help) | ||
{ | ||
base.CreateHelp(help); | ||
help.AddSummary(Summary); | ||
help.AddExample(Example); | ||
help.AddParameter("format", "f", "Format of the data (" + nameof(Format.Display) + ") " + DisplayEnumOptions<Format>()); | ||
} | ||
|
||
|
||
|
||
protected override void ExecuteInternal() | ||
{ | ||
base.ExecuteInternal(); | ||
var format = GetArgParameterOrConfigEnum("format", "f", Format.Display); | ||
|
||
using (var ad = GetActiveDirectory()) | ||
{ | ||
var objects = ad.GetAll().OrEmpty(); | ||
foreach (var obj in objects.OrderBy(o => o.DistinguishedName, StringComparer.OrdinalIgnoreCase)) | ||
{ | ||
if (IsValidObject(obj)) log.Info(Display(obj, format)); | ||
} | ||
} | ||
|
||
} | ||
|
||
protected virtual string Display(ActiveDirectoryObject obj, Format format) | ||
{ | ||
if (format == Format.Display) return ObjName(obj) + " --> " + obj.DistinguishedName; | ||
if (format == Format.TAB) return ObjName(obj) + "\t" + obj.DistinguishedName; | ||
throw new NotImplementedException($"format [{format}] is not implemented"); | ||
} | ||
|
||
protected string ObjName(ActiveDirectoryObject obj) => obj.LogonNamePreWindows2000 ?? obj.LogonName ?? obj.Name; | ||
|
||
protected abstract bool IsValidObject(ActiveDirectoryObject obj); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListComputers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (c) 2021 Steven Foster ([email protected]) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using HavokMultimedia.Utilities.Console.External; | ||
|
||
namespace HavokMultimedia.Utilities.Console.Commands | ||
{ | ||
public class ActiveDirectoryListComputers : ActiveDirectoryListBase | ||
{ | ||
protected override string Summary => "Lists all computer names in an ActiveDirectory"; | ||
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsComputer; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListGroups.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (c) 2021 Steven Foster ([email protected]) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using HavokMultimedia.Utilities.Console.External; | ||
|
||
namespace HavokMultimedia.Utilities.Console.Commands | ||
{ | ||
public class ActiveDirectoryListGroups : ActiveDirectoryListBase | ||
{ | ||
protected override string Summary => "Lists all group names in an ActiveDirectory"; | ||
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsGroup; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (c) 2021 Steven Foster ([email protected]) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using HavokMultimedia.Utilities.Console.External; | ||
|
||
namespace HavokMultimedia.Utilities.Console.Commands | ||
{ | ||
public class ActiveDirectoryListUsers : ActiveDirectoryListBase | ||
{ | ||
protected override string Summary => "Lists all user names in an ActiveDirectory"; | ||
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
HavokMultimedia.Utilities.Console/Commands/ActiveDirectoryListUsersOfGroup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright (c) 2021 Steven Foster ([email protected]) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using HavokMultimedia.Utilities.Console.External; | ||
|
||
namespace HavokMultimedia.Utilities.Console.Commands | ||
{ | ||
public class ActiveDirectoryListUsersOfGroup : ActiveDirectoryListBase | ||
{ | ||
protected override string Summary => "Lists all user names that are members of the specified group in an ActiveDirectory"; | ||
protected override string Example => base.Example + " M?Group*"; | ||
protected override bool IsValidObject(ActiveDirectoryObject obj) => obj.IsUser && obj.MemberOfNames.Any(o => o.EqualsWildcard(group, true)); | ||
private string group; | ||
protected override void ExecuteInternal() | ||
{ | ||
group = GetArgValueTrimmed(0); | ||
log.Debug($"{nameof(group)}: {group}"); | ||
if (group == null) throw new ArgsException(nameof(group), $"No {nameof(group)} specified"); | ||
|
||
base.ExecuteInternal(); | ||
} | ||
protected override string Display(ActiveDirectoryObject obj, Format format) | ||
{ | ||
var matchedGroups = new List<string>(); | ||
foreach (var m in obj.MemberOfNames) | ||
{ | ||
if (m.EqualsWildcard(group, true)) matchedGroups.Add(m); | ||
} | ||
if (format == Format.Display) return base.Display(obj, format) + " (" + matchedGroups.ToStringDelimited(",") + ")"; | ||
if (format == Format.TAB) return base.Display(obj, format) + "\t" + matchedGroups.ToStringDelimited(","); | ||
return base.Display(obj, format); | ||
} | ||
|
||
} | ||
} |