Solved

Script to check if a user has a profile on a server

Posted on 2013-01-24
6
512 Views
Last Modified: 2013-01-30
We have 200+ Windows 2008 and 2008 R2 servers and about 150 tech support personnel who can log into the server.  Our tech support personnel roster has a high turnover, so when someone leaves the company, we have the possibility of checking these 200+ servers for local profiles of the tech support personnel and then deleting them.

Since this is a ridiculously tedious process, we want to script this somehow.  All the servers belong to one domain, so I'm thinking checking AD for a server list and then running this server list against the username of the tech support guy who left.  And then we could potentially script the deletion of the profile or go in manually (script is preferrable).

Please don't tell me to use roaming profiles or folder redirection as my solution.  That is in the process but due to the complexity of our environment is not an easy deployment.  Any suggestions?
0
Comment
Question by:sedberg1
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 38815244
Here is a quick and dirty script that will do this for you. Basically I have a single computer name in there but you can modify the get-qadcomputer part to use an LDAP filter, OSversion, etc to pull computers from AD.

get-qadcomputer computername | foreach {$servername=$_.name
$test = test-path \\$servername\c$\users\USERNAME
"$servername, $test$" >> c:\localprofile.txt
}
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 38816861
First thing is to build a list of the computers.  If you already have a text file with this then we don't have to scan AD computers accounts and test if they're valid.  I'll make that assumption for this script.
Import-Module ActiveDirectory
$username = Read-Host "Enter the username you want to search for"
$userSID = (Get-ADUser -filter * | Where {$_.samaccountname -eq "$username"}).SID.ToString()
#$computers = Get-ADComputer -filter * | Where {Test-Connection $_.DNSHostName -count 1 -quiet} | ForEach-Object {$_.DNSHostName} | Sort-Object
#or if you already have a list of good computers
$computers = Get-Content C:\test\computerlist.txt
ForEach ($computer in $computers)
{
  $userprofile = Get-WmiObject Win32_UserProfile -computername $computer -filter "SID = '$userSID'"
  If ($userprofile)
  {
    $userprofile.delete()
    Write-Output "User profile deleted for ""$username"" from ""$computer"""
  }
}

Open in new window

What I've posted above will only handle a single username at a time, but we could expand it to handle multiple if needed.  I'm assuming you're only worried about AD users and not local users.
0
 

Author Comment

by:sedberg1
ID: 38818754
@footech, thanks for the script.

I do have a list of computernames already but am trying it out with just 1 computername in the text file right now.  When I run the script, this is what I get:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\users\mram\Desktop\profile2.ps1:9 char:31
+   $userprofile = Get-WmiObject <<<<  Win32_UserProfile -computername $computer -filter "SID = '$userSID'"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Open in new window


But if I run the Get-WmiObject Win32_UserProfile command just by itself, I can see the profile there.
0
Is Your AD Toolbox Looking More Like a Toybox?

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.

 

Assisted Solution

by:sedberg1
sedberg1 earned 0 total points
ID: 38818805
Never mind.  There was something wrong with my computerlist.txt file that I was inputting.

One thing I had to change was how I derived the SID.  The SID.ToString() didn't work, so I had to do this:

import-module activedirectory
$username = Read-Host "Enter the username you want to search for"
$objUser = New-Object System.Security.Principal.NTAccount("mydomain.local", "$username") 
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value

Open in new window


Otherwise, it worked awesome.  Thanks.
0
 
LVL 39

Expert Comment

by:footech
ID: 38818916
Odd that that part didn't work for you, I've never encountered any issues with it.  But I'm glad you found another way that would work for you.
0
 

Author Closing Comment

by:sedberg1
ID: 38834511
Part of my code worked.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Looking for a MFP for a small office network 26 123
Office 365 & Microsoft Azure 8 48
Scripts to List File modified in the past week 10 31
MDM vs GPO 16 23
This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

932 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

11 Experts available now in Live!

Get 1:1 Help Now