[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Powershell DSquery variable

Posted on 2013-12-17
11
Medium Priority
?
1,077 Views
Last Modified: 2013-12-18
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.
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

Open in new window


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?
0
Comment
Question by:levertm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39724554
You could probably go with:
$domain = (gwmi WIN32_ComputerSystem).Domain

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.
0
 

Author Comment

by:levertm
ID: 39724605
How does that fit into my current command? Sorry im kind of new to powershell.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39724713
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

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:levertm
ID: 39724839
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

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 39724869
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 ?
0
 

Author Comment

by:levertm
ID: 39724900
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?
0
 
LVL 29

Expert Comment

by:becraig
ID: 39725097
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
0
 

Author Comment

by:levertm
ID: 39725727
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.
0
 

Author Comment

by:levertm
ID: 39727209
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 :)
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 39727305
Ok this is a little dirty but should do what you need:
if the child OU under Accounts matches the domain as well you should be able to just use $use[0] for that value.


$domain = (gwmi WIN32_ComputerSystem).Domain
$use= $domain.split(".")
$user = $use[0]  + "-USERS"
$FQDN = "DC=" + $Domain -Replace("\.",",DC=")
DSQUERY USER "OU=$user,OU=cars,OU=ACCOUNTS,$FQDN" -limit 999 | DSGET USER -SAMID | % {$_.Trim()} | select -Skip 1 | Out-File -encoding utf8 userslist.txt

Open in new window

0
 

Author Comment

by:levertm
ID: 39727428
Worked like magic. Thanks Becraig for the help :)
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question