Solved

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

Posted on 2013-01-24
6
535 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
[X]
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
  • 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 40

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 40

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

733 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