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
Solved

PowerShell command to get the drives in an OU

Posted on 2013-06-27
11
467 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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