Solved

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

Posted on 2013-01-24
6
511 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
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 …
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

758 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

24 Experts available now in Live!

Get 1:1 Help Now