Solved

PowerShell command to get the drives in an OU

Posted on 2013-06-27
11
461 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 68

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 68

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

19 Experts available now in Live!

Get 1:1 Help Now