Solved

PowerShell command to get the drives in an OU

Posted on 2013-06-27
11
471 Views
Last Modified: 2013-07-03
Hello experts, I am trying to find a way to use a powershell command such as Get-PSDrive to get all the drives and their space in our "Servers" OU.  I saw there wasn't a -Searchbase parameter.  Does anyone know if you can use a CSV to run this command on all of your servers?  Thanks in advance!
0
Comment
Question by:j0s3ph
[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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 166 total points
ID: 39281380
Get-PSDrive lists all drives in the machine, why OU is required?

GC servers.csv | Get-PSDrive

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39281412
If you have powershell activedirectory module (Win 2008 R2) then you can try..
Import-Module ActiveDirectory
Function Get-DiskSpace
{Param ([string[]]$servers)
   Foreach ($s in $servers)
    {
      Get-WmiObject -Class win32_volume -cn $s |
 
       Select-Object @{LABEL='Comptuer';EXPRESSION={$s}},
 
         driveletter, label,
 
         @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}}
 
    } #end foreach $s
 
} #end function Get-DiskSpace

Get-ADComputer -Filter * -SearchBase "OU=Computers,OU=Test,DC=Domain,DC=com" | 
		% {Get-DiskSpace $_.Name} |
			Export-Csv C:\Temp\Report.csv -NTI

Open in new window

Ref :  http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/08/use-powershell-to-create-a-report-displaying-free-disk-space.aspx
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39281450
replace the $OU with your required OU:
cls
Enable-PSRemoting
$OU = 'laptop'
Get-QADComputer -SizeLimit 0 | where{$_.dn -match $OU} | %{
	icm $_.Name {Get-PSDrive} | Select @{Name="Computer";Expression={$_.Name}}, Name, Root, Free, Used 
} | Export-Csv C:\Report.csv -NTI

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 40

Expert Comment

by:Subsun
ID: 39281488
@sedgwick, That may not work as expected, for example if you have two OU's named 'laptop' then it will pull information from both OU's. Also the Export-Csv is placed inside the loop so the report file will get overwritten for each computer result..
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39281494
@Subsun
that's why he needs to put the exact OU name.
if the name of the OU is Client Servers, then he needs to use 'Client Servers' and not just 'Servers'.

cls
Enable-PSRemoting
$OU = 'put exact OU name here'
Get-QADComputer -SizeLimit 0 | where{$_.dn -match $OU} | %{
	$server = $_.Name
	icm $server {Get-PSDrive} | Select @{Name="Computer";Expression={$server}}, Name, Root, Free, Used 
} | Export-Csv C:\Report.csv -NTI

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 167 total points
ID: 39281513
Again if there is more than one OU named 'Client Servers' then the result may not be as expected..

For example..
"OU=Client Servers,OU=Computers,OU=Test,DC=Domain,DC=com"
"OU=Client Servers,DC=Domain,DC=com"

but if there is only one OU named 'Client Servers' then it will work..

Hope it's clear..

I would use -SearchRoot parameter with Get-QADComputer to get the details from exact OU.
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 39281602
You should also filter Get-PsProvider for the provider FileSystem:
   Get-PSDrive -PsProvider FileSystem
to make sure you do not get Alias, Registry and such.
0
 

Author Comment

by:j0s3ph
ID: 39294120
Hey guys, thanks for the replies.  I am interested in keeping this script simple.  I like the idea of "GC servers.csv | Get-PSDrive ."  Is there an easy way to create the servers.csv file that is only the fqdn of the servers in a specific OU?  Maybe is that the best way to get this list of servers with their drives?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39294200
Forget it. Get-PSDrive does NOT work remotely. You have to invoke it with invoke-command (icm), to execute it locally in  a remote session.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39294229
The list of servers in an OU can be retrieved as shown already:
Get-QADComputer -SizeLimit 0 -SearchRoot "OU=WhichEver;OU=Computers;DC=domain;DC=com" | select Name, DN

Open in new window

0
 

Author Comment

by:j0s3ph
ID: 39297682
Thanks for all of your help!
I will credit multiple users.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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