Solved

PowerShell command to get the drives in an OU

Posted on 2013-06-27
11
465 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:
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 69

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 69

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 69

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A brief introduction to what I consider to be the best editor for PowerShell.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now