Solved

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

Posted on 2013-01-24
6
523 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A safe way to clean winsxs folder from your windows server 2008 R2 editions
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
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 …

839 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