Created
April 13, 2018 19:08
-
-
Save def-/0cb632450a70cbcfabe9d189ac783f16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os, osproc, strutils, rdstdin | |
type UserNotFound = object of Exception | |
const | |
userFields = ["Full Name", "Account active", "Account expires", "Password last set"] | |
userNotFound = "The user name could not be found" | |
proc getDomainUser(username: string): seq[string] = | |
result = @[] | |
let netoutput = execProcess("net user $# /domain" % username) | |
if userNotFound in netoutput: | |
raise newException(UserNotFound, "No such user was found.") | |
for line in netoutput.splitLines: | |
for field in userFields: | |
if field in line: | |
result.add(line) | |
proc getAllDomainUsers: seq[string] = | |
result = execProcess("net user /domain").splitWhitespace()[17..^5] | |
proc findUsers(username: string, userlist: seq[string]): seq[string] = | |
result = @[] | |
for line in userlist: | |
if username in line: | |
result.add(line) | |
proc getUsername: string = | |
if paramCount() >= 1: | |
result = paramStr(1) | |
else: | |
result = readLineFromStdin "Please Enter a Username: " | |
proc getUserNum(rangenum: int): int = | |
while true: | |
result = stdin.readLine().parseInt() | |
if result < 0 or result > rangenum: | |
echo "Invalid choice. Please choose a valid number." | |
continue | |
break | |
proc getUserInfo(username: string) = | |
try: | |
for line in getDomainUser(username): | |
echo line | |
except UserNotFound: | |
let allusers = getAllDomainUsers() | |
let probusers: seq[string] = findUsers(username, allusers) | |
if probusers.len == 0: | |
echo "No Users Found." | |
elif probusers.len == 1: | |
getUserInfo(probusers[0]): | |
elif probusers.len > 1: | |
echo "More than One User Found. Please Enter a Number:\n" | |
for x, name in probusers: | |
echo x, " -- ", name | |
let usernum = getUserNum(probusers.len) | |
echo "" | |
getUserInfo(probusers[usernum]) | |
var user = "" | |
while user == "": | |
user = getUsername() | |
getUserInfo(user) | |
discard readLineFromStdin "\nPress Enter to Exit." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment