Terminal services profile

Hi,

anyone can provide with a simple code to delete Terminal services Profile path  entries  for listed (csv.) users in AD?
kuzumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kuzumAuthor Commented:
any ideas please
0
kuzumAuthor Commented:
anyone can provide with a simple code to delete Terminal services Profile path  entries  for users in my .csv file for AD
0
oBdACommented:
Are you talking about a "real" csv as input file, with several columns, including a header line, or just a simple text file with one user name per line?
If the former, please provide a sample with a John Doe account, including the header.
This one is for the latter, assuming one logon name per line:
Get-Content "C:\Temp\test.txt" |
	% {
		"Processing '$($_)' ..."
		If ($ADUser = Get-ADUser -Identity $_) {
			$LdapUser = [adsi]"LDAP://$($ADUser.DistinguishedName)"
			$LdapUser.PsBase.RefreshCache()
			$LdapUser.PsBase.InvokeSet("TerminalServicesProfilePath", "")
			$LdapUser.SetInfo()
		}
	}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kuzumAuthor Commented:
basically, I have a list of users in excel format whose terminal services profile path needs to be cleared. Can you explain your script a bit please
0
oBdACommented:
The terminal services profile path isn't a regular AD attribute (it's in a binary blob userParameters), but you can change it with the adsi provider (a shortcut aka accelerator for the System.DirectoryServices.DirectoryEntry class).
So the script first uses the regular "Get-ADUser" to retrieve the user account (especially the DistinguishedName), then gets the adsi representation of the user, and sets the terminal services profile path with the methods provided by the class.
0
kuzumAuthor Commented:
thanks, does home folder entry causes any issues? do we need to mention that too? or Simply running your code be enough to clear the path for listed users. I noticed on some forums that they had issues as those users had home drives?

thanks again
0
oBdACommented:
Hm? Please provide the links to those issue reports. It worked fine in my lab, and I don't see any relation at all to the home drive. You'd need to use a similar method (TerminalServicesHomeDrive and TerminalServicesHomeDirectory) to set the TS specific home folder, but that should be it.
0
kuzumAuthor Commented:
sorry for confusion, of it works fine in your lab I'm sure your solution is accurate.

https://jthys.wordpress.com/2011/05/06/powershell-clear-tsprofilepath-in-active-directory/
0
oBdACommented:
Don't see any issues with this.
0
kuzumAuthor Commented:
Hi oBdA,

I will run your code today, is it possible to have a logs to be created by the code? would be good idea to have it for my management

thanks
0
oBdACommented:
$LogFile = "C:\Temp\result.csv"
Get-Content "C:\Temp\test.txt" |
	% {
		"Processing '$($_)' ... " | Write-Host -NoNewLine
		$Log = $_ | Select-Object -Property @{Name="User"; Expression={$_}}, @{Name="Result"; Expression={"OK"}}
		Try {
			$ADUser = Get-ADUser -Identity $_ -ErrorAction Stop
			$LdapUser = [adsi]"LDAP://$($ADUser.DistinguishedName)"
			$LdapUser.PsBase.RefreshCache()
			$LdapUser.PsBase.InvokeSet("TerminalServicesProfilePath", "")
			$LdapUser.SetInfo() | Out-Null
			$Log.Result | Write-Host -ForegroundColor Green
		} Catch {
			$Log.Result = $_.Exception.Message
			$Log.Result | Write-Host -ForegroundColor Red
		}
		$Log | Write-Output
	} | Export-Csv -Path $LogFile -NoTypeInformation

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kuzumAuthor Commented:
thanks, do I need to install AD module first? I'm running this on my windows 8 machine

The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included
, verify that the path is correct and try again.

regards
0
oBdACommented:
Remote Server Administration Tools (RSAT) for Windows operating systems
https://support.microsoft.com/en-us/kb/2693643

And depending on the PS version, you might need an "Import-Module ActiveDirectory" at the beginning of the script.
0
kuzumAuthor Commented:
thanks, it seems worked. before I close the request, Do I need to get rid off home folder entry too? Or removing the profile path would be enough for my scenario?  What I am trying to achieve is to remove the entry so users no longer affected for login when my server is switched off. thanks
0
oBdACommented:
Well, if the TS specific home folder is set as well in the Remote Desktop tab (not in the regular Profile tab), and it points to the server that is about to be switches off, then those entries would obviously need to be cleared as well.
This would work the same way:
$LogFile = "C:\Temp\result.csv"
Get-Content "C:\Temp\test.txt" |
	% {
		"Processing '$($_)' ... " | Write-Host -NoNewLine
		$Log = $_ | Select-Object -Property @{Name="User"; Expression={$_}}, @{Name="Result"; Expression={"OK"}}
		Try {
			$ADUser = Get-ADUser -Identity $_ -ErrorAction Stop
			$LdapUser = [adsi]"LDAP://$($ADUser.DistinguishedName)"
			$LdapUser.PsBase.RefreshCache()
			$LdapUser.PsBase.InvokeSet("TerminalServicesHomeDrive", "")
			$LdapUser.PsBase.InvokeSet("TerminalServicesHomeDirectory", "")
			$LdapUser.SetInfo() | Out-Null
			$Log.Result | Write-Host -ForegroundColor Green
		} Catch {
			$Log.Result = $_.Exception.Message
			$Log.Result | Write-Host -ForegroundColor Red
		}
		$Log | Write-Output
	} | Export-Csv -Path $LogFile -NoTypeInformation

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.