levertm
asked on
Powershell DSquery variable
Hello,
Can anyone help me convert the following into a variable on the user domain part? I need to be able to launch this powershell command from any child domain or even the root domain. So basically the variable would take into account the current domain and fill in the blanks.
What this does is it queries the OU and exports a list of users into a text file. it works perfectly. How can I get this command into a variable so i can launch it from any child domain?
Can anyone help me convert the following into a variable on the user domain part? I need to be able to launch this powershell command from any child domain or even the root domain. So basically the variable would take into account the current domain and fill in the blanks.
DSQUERY USER "OU=cars-USERS,OU=cars,OU=ACCOUNTS,DC=cars,DC=automobiles,DC=roads,DC=CA" -limit 999 | DSGET USER -SAMID | % {$_.Trim()} | select -Skip 1 | Out-File -encoding utf8 userslist.txt
What this does is it queries the OU and exports a list of users into a text file. it works perfectly. How can I get this command into a variable so i can launch it from any child domain?
ASKER
How does that fit into my current command? Sorry im kind of new to powershell.
Something like this should work:
$domain = (gwmi WIN32_ComputerSystem).Domain
$FQDN = "DC=" + $Domain -Replace("\.",",DC=")
DSQUERY USER "OU=cars-USERS,OU=cars,OU=ACCOUNTS,$FQDN" -limit 999 | DSGET USER -SAMID | % {$_.Trim()} | select -Skip 1 | Out-File -encoding utf8 userslist.txt
ASKER
That works for the Domain part, thanks. How about the OU part, how can I turn that into a variable as well? As you'll notice the "cars-users" OU matches the "DC=cars" part if that makes sense? I need that first OU container to be a variable.
$domain = (gwmi WIN32_ComputerSystem).Domain
$FQDN = "DC=" + $Domain -Replace("\.",",DC=")
DSQUERY USER "OU=cars-USERS,OU=cars,OU=ACCOUNTS,$FQDN" -limit 999 | DSGET USER -SAMID | % {$_.Trim()} | select -Skip 1 | Out-File -encoding utf8 userslist.txt
How do you plan to pipe in the OU variable (list of OUs from a text file ?) ?
Have you thought of just looking at using AD cmdlets ?
Have you thought of just looking at using AD cmdlets ?
ASKER
Hmm not sure, I just know the first OU "cars-users" matches the domain name "cars", would there not be a way to add a variable "%domainname-users%" or something similar?
ok so if I am reading you correctly you want to:
1) Find all the OUs in accounts, e.g.
OU=cars,OU=ACCOUNTS,$FQDN"
2) Create a query based on each childOU found in the parent e.g
OU=cars-USER
3) Query that OUS for a list of all the users in the child OU
1) Find all the OUs in accounts, e.g.
OU=cars,OU=ACCOUNTS,$FQDN"
2) Create a query based on each childOU found in the parent e.g
OU=cars-USER
3) Query that OUS for a list of all the users in the child OU
ASKER
I think so if I understand your steps. To make it clearer I need to be able to run this on any given child domain. In this case the domain is called cars, so the first OU is called "cars-users" .With your first modification I'm able to get the domain "cars" via the variable, I now need to add a variable for the first you "cars-users" as the other OU such as "accounts" is same throughout the other child domains.
To conclude, there might be a domain called "trucks" and its first OU would be "trucks-users".. The script would need to work in this domain as well.
To conclude, there might be a domain called "trucks" and its first OU would be "trucks-users".. The script would need to work in this domain as well.
ASKER
To make it even easier... I just need to replace "Cars-users" with "Domainname-users"
I tried
"$Domain-users" but in theory that would give me "xxx.yyy.ca-users" correct? I just need "xxx-users"
Hope this makes it easier :)
I tried
"$Domain-users" but in theory that would give me "xxx.yyy.ca-users" correct? I just need "xxx-users"
Hope this makes it easier :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked like magic. Thanks Becraig for the help :)
$domain = (gwmi WIN32_ComputerSystem).Doma
This will give you the domain of the computer it is being run on.
You can then split the domain fqdn if you need to.