Solved

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

Posted on 2013-01-24
6
546 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
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.
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…

717 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