Powershell : Finding Remote Desktop Home Folders.

I am working on a project to migrate a Windows Cluster. I need to find any AD accounts that are pointing to the older cluster location in Remote Desktop Services Home Folder.

 

I have pilfered this code from the interwebs : https://www.petri.com/powershell-problem-solver-active-directory-remote-desktop-settings

 

[code language="powershell"]
Function Get-RDUserSetting {
 
[cmdletbinding(DefaultParameterSetName="SAM")]
 
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter a user's sAMAccountName",
ValueFromPipeline,ParameterSetName="SAM")]
[ValidateNotNullorEmpty()]
[Alias("Name")]
[string]$SAMAccountname,
[Parameter(ParameterSetName="SAM")]
[string]$SearchRoot,
 
[Parameter(Mandatory,HelpMessage="Enter a user's distingished name",
ValueFromPipelineByPropertyName,ParameterSetName="DN")]
[ValidateNotNullorEmpty()]
[Alias("DN")]
[string]$DistinguishedName,
 
[string]$Server
 
)
 
Begin {
 Write-Verbose "Starting $($MyInvocation.MyCommand)"
 Write-Verbose ($PSBoundParameters | Out-String)
 #remote desktop properties
 $TSSettings = @("TerminalServicesProfilePath","TerminalServicesHomeDirectory","TerminalServicesHomeDrive")
} #Begin
 
Process {
 Write-Verbose "Using parameter set $($PSCmdlet.ParameterSetName)"
 Switch ($PSCmdlet.ParameterSetName) {
 
 "SAM" {
 Write-Verbose "Retrieving distinguishedname for $samAccountname"
 $searcher = New-Object DirectoryServices.DirectorySearcher
 $searcher.Filter = "(&(objectcategory=person)(objectclass=user)(samAccountname=$sAMAccountname))"
 Write-Verbose $searcher.filter
 if ($SearchRoot) {
 Write-Verbose "Searching from $SearchRoot"
 if ($Server) {
 $searchPath = "LDAP://$server/$SearchRoot"
 }
 else {
 $searchPath = "LDAP://$SearchRoot"
 }
 $r = New-Object System.DirectoryServices.DirectoryEntry $SearchPath
 
 $searcher.SearchRoot = $r
 }
 $user = $searcher.FindOne().GetDirectoryEntry()
 } 
 "DN" {
 Write-Verbose "Processing $DistinguishedName"
 if ($server) {
 Write-Verbose "Connecting to $Server"
 [ADSI]$User = "LDAP://$Server/$DistinguishedName"
 }
 else {
 [ADSI]$User = "LDAP://$DistinguishedName"
 }
 }
 } #close Switch
 
 if ($user.path) {
 #initialize a hashtable
 Try {
 $hash=[ordered]@{
 DistinguishedName = $User.DistinguishedName.Value
 Name = $user.name.Value
 samAccountName = $user.samAccountName.value
 AllowLogon = $user.psbase.InvokeGet("AllowLogon") -as [Boolean]
 }
 
 foreach ($property in $TSSettings) {
 $hash.Add($property,$user.psbase.invokeGet($property))
 
 } #foreach
 
 #create an object
 New-Object -TypeName PSObject -Property $hash
 }
 Catch {
 Write-Warning "Failed to retrieve remote desktop settings for $Distinguishedname. $($_.exception.message)"
 }
 } #if user found
 else {
 Write-Warning "Failed to find user $DistinguishedName. $($_.exception.message)"
 }
 
} #Process
 
End {
 Write-Verbose "Ending $($MyInvocation.MyCommand)"
} #End
 
} #end function
[/code]

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *