{"id":745,"date":"2018-01-26T10:18:58","date_gmt":"2018-01-26T10:18:58","guid":{"rendered":"http:\/\/giraffeit.com\/?p=745"},"modified":"2018-01-26T10:24:01","modified_gmt":"2018-01-26T10:24:01","slug":"powershell-finding-remote-desktop-home-folders","status":"publish","type":"post","link":"https:\/\/giraffeit.com\/index.php\/2018\/01\/26\/powershell-finding-remote-desktop-home-folders\/","title":{"rendered":"Powershell : Finding Remote Desktop Home Folders."},"content":{"rendered":"<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p>I have pilfered this code from the interwebs :\u00a0https:\/\/www.petri.com\/powershell-problem-solver-active-directory-remote-desktop-settings<\/p>\n<p>&nbsp;<\/p>\n<pre>[code language=\"powershell\"]\r\nFunction Get-RDUserSetting {\r\n \r\n[cmdletbinding(DefaultParameterSetName=\"SAM\")]\r\n \r\nParam(\r\n[Parameter(Position=0,Mandatory,HelpMessage=\"Enter a user's sAMAccountName\",\r\nValueFromPipeline,ParameterSetName=\"SAM\")]\r\n[ValidateNotNullorEmpty()]\r\n[Alias(\"Name\")]\r\n[string]$SAMAccountname,\r\n[Parameter(ParameterSetName=\"SAM\")]\r\n[string]$SearchRoot,\r\n \r\n[Parameter(Mandatory,HelpMessage=\"Enter a user's distingished name\",\r\nValueFromPipelineByPropertyName,ParameterSetName=\"DN\")]\r\n[ValidateNotNullorEmpty()]\r\n[Alias(\"DN\")]\r\n[string]$DistinguishedName,\r\n \r\n[string]$Server\r\n \r\n)\r\n \r\nBegin {\r\n Write-Verbose \"Starting $($MyInvocation.MyCommand)\"\r\n Write-Verbose ($PSBoundParameters | Out-String)\r\n #remote desktop properties\r\n $TSSettings = @(\"TerminalServicesProfilePath\",\"TerminalServicesHomeDirectory\",\"TerminalServicesHomeDrive\")\r\n} #Begin\r\n \r\nProcess {\r\n Write-Verbose \"Using parameter set $($PSCmdlet.ParameterSetName)\"\r\n Switch ($PSCmdlet.ParameterSetName) {\r\n \r\n \"SAM\" {\r\n Write-Verbose \"Retrieving distinguishedname for $samAccountname\"\r\n $searcher = New-Object DirectoryServices.DirectorySearcher\r\n $searcher.Filter = \"(&amp;(objectcategory=person)(objectclass=user)(samAccountname=$sAMAccountname))\"\r\n Write-Verbose $searcher.filter\r\n if ($SearchRoot) {\r\n Write-Verbose \"Searching from $SearchRoot\"\r\n if ($Server) {\r\n $searchPath = \"LDAP:\/\/$server\/$SearchRoot\"\r\n }\r\n else {\r\n $searchPath = \"LDAP:\/\/$SearchRoot\"\r\n }\r\n $r = New-Object System.DirectoryServices.DirectoryEntry $SearchPath\r\n \r\n $searcher.SearchRoot = $r\r\n }\r\n $user = $searcher.FindOne().GetDirectoryEntry()\r\n } \r\n \"DN\" {\r\n Write-Verbose \"Processing $DistinguishedName\"\r\n if ($server) {\r\n Write-Verbose \"Connecting to $Server\"\r\n [ADSI]$User = \"LDAP:\/\/$Server\/$DistinguishedName\"\r\n }\r\n else {\r\n [ADSI]$User = \"LDAP:\/\/$DistinguishedName\"\r\n }\r\n }\r\n } #close Switch\r\n \r\n if ($user.path) {\r\n #initialize a hashtable\r\n Try {\r\n $hash=[ordered]@{\r\n DistinguishedName = $User.DistinguishedName.Value\r\n Name = $user.name.Value\r\n samAccountName = $user.samAccountName.value\r\n AllowLogon = $user.psbase.InvokeGet(\"AllowLogon\") -as [Boolean]\r\n }\r\n \r\n foreach ($property in $TSSettings) {\r\n $hash.Add($property,$user.psbase.invokeGet($property))\r\n \r\n } #foreach\r\n \r\n #create an object\r\n New-Object -TypeName PSObject -Property $hash\r\n }\r\n Catch {\r\n Write-Warning \"Failed to retrieve remote desktop settings for $Distinguishedname. $($_.exception.message)\"\r\n }\r\n } #if user found\r\n else {\r\n Write-Warning \"Failed to find user $DistinguishedName. $($_.exception.message)\"\r\n }\r\n \r\n} #Process\r\n \r\nEnd {\r\n Write-Verbose \"Ending $($MyInvocation.MyCommand)\"\r\n} #End\r\n \r\n} #end function\r\n[\/code]<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. &nbsp; I have pilfered this code from the interwebs :\u00a0https:\/\/www.petri.com\/powershell-problem-solver-active-directory-remote-desktop-settings &nbsp; [code language=&#8221;powershell&#8221;] Function Get-RDUserSetting { [cmdletbinding(DefaultParameterSetName=&#8221;SAM&#8221;)] Param( [Parameter(Position=0,Mandatory,HelpMessage=&#8221;Enter a user&#8217;s sAMAccountName&#8221;, ValueFromPipeline,ParameterSetName=&#8221;SAM&#8221;)] &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/giraffeit.com\/index.php\/2018\/01\/26\/powershell-finding-remote-desktop-home-folders\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Powershell : Finding Remote Desktop Home Folders.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-745","post","type-post","status-publish","format-standard","hentry","category-bog"],"_links":{"self":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/comments?post=745"}],"version-history":[{"count":3,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/745\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/745\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/media?parent=745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/categories?post=745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/tags?post=745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}