{"id":749,"date":"2018-01-26T10:26:35","date_gmt":"2018-01-26T10:26:35","guid":{"rendered":"http:\/\/giraffeit.com\/?p=749"},"modified":"2018-04-19T10:11:00","modified_gmt":"2018-04-19T10:11:00","slug":"powershell-changing-remote-desktop-home-folders","status":"publish","type":"post","link":"https:\/\/giraffeit.com\/index.php\/2018\/01\/26\/powershell-changing-remote-desktop-home-folders\/","title":{"rendered":"Powershell : changing Remote Desktop Home Folders."},"content":{"rendered":"<p>In my last blog I posted about finding the Remote Desktop Home Folders. Best now post how to change them &#8230;<\/p>\n<pre class=\"lang:ps decode:true \" >Function Set-RDUserSetting {\r\n[cmdletbinding(SupportsShouldProcess)]\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[boolean]$AllowLogon,\r\n[Alias(\"Profile\")]\r\n[string]$TerminalServicesProfilePath,\r\n[Alias(\"HomeDirectory\")]\r\n[string]$TerminalServicesHomeDirectory,\r\n[Alias(\"HomeDrive\")]\r\n[string]$TerminalServicesHomeDrive,\r\n\r\n[string]$Server,\r\n\r\n[switch]$Passthru\r\n)\r\n\r\nBegin {\r\nWrite-Verbose \"Starting $($MyInvocation.MyCommand)\"\r\nWrite-Verbose ($PSBoundParameters | out-string)\r\n#remote desktop properties\r\n$TSSettings = @(\"TerminalServicesProfilePath\",\"TerminalServicesHomeDirectory\",\"TerminalServicesHomeDrive\")\r\n} #Begin\r\n\r\nProcess {\r\n\r\nWrite-Verbose \"Using parameter set $($PSCmdlet.ParameterSetName)\"\r\nSwitch ($PSCmdlet.ParameterSetName) {\r\n\r\n\"SAM\" {\r\nWrite-Verbose \"Retrieving distinguishedname for $samAccountname\"\r\n$searcher = New-Object DirectoryServices.DirectorySearcher\r\n$searcher.Filter = \"(&amp;amp;(objectcategory=person)(objectclass=user)(samAccountname=$sAMAccountname))\"\r\nWrite-Verbose $searcher.filter\r\nif ($SearchRoot) {\r\nWrite-Verbose \"Searching from $SearchRoot\"\r\nif ($Server) {\r\n$searchPath = \"LDAP:\/\/$server\/$SearchRoot\"\r\n}\r\nelse {\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\nWrite-Verbose \"Processing $DistinguishedName\"\r\nif ($server) {\r\nWrite-Verbose \"Connecting to $Server\"\r\n[ADSI]$User = \"LDAP:\/\/$Server\/$DistinguishedName\"\r\n}\r\nelse {\r\n[ADSI]$User = \"LDAP:\/\/$DistinguishedName\"\r\n}\r\n}\r\n} #close Switch\r\n\r\nif ($user.path) {\r\nif ($PSBoundParameters.ContainsKey(\"AllowLogon\")) {\r\nWrite-Verbose \"Configuring AllowLogon\"\r\n$user.psbase.invokeSet(\"AllowLogon\",$AllowLogon -as [int])\r\n}\r\nforeach ($property in $TSSettings) {\r\nif ($PSBoundParameters.ContainsKey($property)) {\r\nWrite-Verbose \"Setting $property = $($PSBoundParameters[$property])\"\r\n$user.psbase.invokeSet($property,$PSBoundParameters[$property])\r\n}\r\n}\r\n#commit changes\r\nif ($PSCmdlet.ShouldProcess($DistinguishedName)){\r\n$user.setInfo()\r\n} #Whatif\r\n\r\nif ($Passthru) {\r\n$hash=[ordered]@{\r\nDistinguishedName = $User.DistinguishedName.Value\r\nName = $user.name.Value\r\nsamAccountName = $user.samAccountName.value\r\nAllowLogon = $user.psbase.InvokeGet(\"AllowLogon\") -as [Boolean]\r\n}\r\n\r\nforeach ($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\nNew-Object -TypeName PSObject -Property $hash\r\n}\r\n} #if user found\r\nelse {\r\nWrite-Warning \"Failed to find user $DistinguishedName\"\r\n}\r\n\r\n} #Process\r\n\r\nEnd {\r\nWrite-Verbose \"Ending $($MyInvocation.MyCommand)\"\r\n} #End\r\n\r\n} #end function<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my last blog I posted about finding the Remote Desktop Home Folders. Best now post how to change them &#8230; Function Set-RDUserSetting { [cmdletbinding(SupportsShouldProcess)] Param( [Parameter(Position=0,Mandatory,HelpMessage=&#8221;Enter a user&#8217;s sAMAccountName&#8221;, ValueFromPipeline,ParameterSetName=&#8221;SAM&#8221;)] [ValidateNotNullorEmpty()] [Alias(&#8220;Name&#8221;)] [string]$SAMAccountname, [Parameter(ParameterSetName=&#8221;SAM&#8221;)] [string]$SearchRoot, [Parameter(Mandatory,HelpMessage=&#8221;Enter a user&#8217;s distingished name&#8221;, ValueFromPipelineByPropertyName,ParameterSetName=&#8221;DN&#8221;)] [ValidateNotNullorEmpty()] [Alias(&#8220;DN&#8221;)] [string]$DistinguishedName, [boolean]$AllowLogon, [Alias(&#8220;Profile&#8221;)] [string]$TerminalServicesProfilePath, [Alias(&#8220;HomeDirectory&#8221;)] [string]$TerminalServicesHomeDirectory, [Alias(&#8220;HomeDrive&#8221;)] [string]$TerminalServicesHomeDrive, [string]$Server, [switch]$Passthru ) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/giraffeit.com\/index.php\/2018\/01\/26\/powershell-changing-remote-desktop-home-folders\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Powershell : changing 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-749","post","type-post","status-publish","format-standard","hentry","category-bog"],"_links":{"self":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/749","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=749"}],"version-history":[{"count":3,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/749\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/posts\/749\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/media?parent=749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/categories?post=749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/giraffeit.com\/index.php\/wp-json\/wp\/v2\/tags?post=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}