Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modifying Powershell script to list all Home drive or directories with no AD account associated.

Posted on 2016-09-27
6
Medium Priority
?
181 Views
Last Modified: 2016-09-29
Hi people,

I'd like to know if it is possible to modify the script below which search and list any User home flder which is still exist in the File Server but there is no owner or no AD account owning it:

Get-ADUser -SearchBase "OU=Disabled USERS,OU=Main Office,DC=domain,DC=com" -F * -Pr HomeDirectory | ?{$_.HomeDirectory -match "^\\\\FileServerHQ\\Users"} | Select Name,HomeDirectory,@{N="Size";E={"{0:N2}"  -f ((Get-ChildItem $_.HomeDirectory -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"}} | Export-CSV C:\TEMP\report.csv -NoTypeInformation

Open in new window


Thanks in advance,

This is the series of question that I ask from: https://www.experts-exchange.com/questions/28972587/Modifying-PowerShell-to-list-home-drives-of-disabled-users.html
0
Comment
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41819121
Try..
GCI "\\FileServerHQ\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}},@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\report.csv -NTI

Open in new window

Or
GCI "\\FileServerHQ\Users" | ?{ $_.PSIsContainer } | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}},@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\report.csv -NTI

Open in new window

Script searches through the home folders inside \\FileServerHQ\Users and lists the folders which it fails to find a user in AD.
1
 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 41820834
Hi Subsun,

It returns back with some error:

Select : The "E" key has a type, System.Object[], that is not valid; expected types are {System.String, System.Management.Automation.ScriptBlock}.
At line:3 char:109
+ ... n.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Open in new window


This is the script that I executed:

GCI "\\FileServer06\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}}, @{N="Size";E={"{0:N2}" -f ((Get-ChildItem $_.HomeDirectory -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"} ,@{N="Error";E={$_}}}}} | Export-CSV C:\TEMP\reportOrphanedDir.csv -NoTypeInformation

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 41821025
Try..
GCI "\\FileServer06\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}}, @{N="Size";E={"{0:N2}" -f ((Get-ChildItem $Folder.Fullname -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"}} ,@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\reportOrphanedDir.csv -NoTypeInformation

Open in new window

0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 41821062
@Subsun,

Thanks for the reply.

However, the result is the full dump of my AD account:

DistinguishedName	Enabled	GivenName	Name	ObjectClass	ObjectGUID	SamAccountName	SID	Surname	UserPrincipalName

Open in new window

so not the orphaned directory with the size ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 41821208
Change Get-ADUser $Folder.name

To

Get-ADUser $Folder.name | Out-null
1
 
LVL 8

Author Closing Comment

by:Senior IT System Engineer
ID: 41821222
Thanks Subsun !
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

824 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