?
Solved

PowerShell command to get the drives in an OU

Posted on 2013-06-27
11
Medium Priority
?
488 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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 664 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:Meir Rivkin
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
Industry Leaders: 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!

 
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:Meir Rivkin
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 668 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 71

Assisted Solution

by:Qlemo
Qlemo earned 668 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 71

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 71

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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
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 anti-spam), the admin…
Loops Section Overview

809 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