{"id":176,"date":"2013-01-18T04:35:17","date_gmt":"2013-01-18T04:35:17","guid":{"rendered":"http:\/\/thewiredguy.com\/wordpress\/?p=176"},"modified":"2013-01-18T04:35:17","modified_gmt":"2013-01-18T04:35:17","slug":"give-user-access-to-remote-file-share-via-powershell","status":"publish","type":"post","link":"https:\/\/inullable.in\/blog\/?p=176","title":{"rendered":"Give user access to remote file share via Powershell"},"content":{"rendered":"<p>In a hurry to write this post, so will add explanation later<\/p>\n<p>Recently a fellow colleague asked\u00a0me to help him in doing this in PowerShell, as usual I went to google, much to my disappointment I couldn&#8217;t find a single solution anywhere on web (TechNet, Script Center, Powershell,com etc.)<br \/>\nSo, I decided to sit in the early morning and forge it out myself, by the time to go to office I had this working solution \ud83d\ude42<\/p>\n<p>Let me know if you guys need any help<\/p>\n<pre style=\"font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee; font-size: 12px; border: 1px dashed #999999; line-height: 14px; padding: 5px; overflow: auto; width: 100%;\"><code>$Computer = \"sanil\"\n$user = \"guest\"\n$domain = \"BUILTIN\"\n\ncls\n$s = Get-WmiObject -Class \"Win32_LogicalShareSecuritySetting\" -Filter \"Name='Music'\"\n$sd = $s.GetSecurityDescriptor()\n\n# List all users having access\n$sd.Descriptor.DACL | ForEach-Object {$_.Trustee.Name}\n\n$ACE = ([WMIClass] \"\\$Computerrootcimv2:Win32_ACE\").CreateInstance()\n$Trustee = ([WMIClass] \"\\$Computerrootcimv2:Win32_Trustee\").CreateInstance()\n\n$Trustee.Name = $user\n$Trustee.Domain = $domain\n$Trustee.SIDString =  (new-object security.principal.ntaccount $user).translate([security.principal.securityidentifier]) \n\n$ace.AccessMask = 2032127\n$ace.AceFlags = 3\n$ace.AceType = 0\n$ace.Trustee = $Trustee\n\n# New Secuity Descriptor\n$nsd = ([WMIClass] \"\\$Computerrootcimv2:Win32_SecurityDescriptor\").CreateInstance()\n\n#Copy all except DACLs\n$nsd.Properties[\"ControlFlags\"].Value = $sd.Descriptor.Properties[\"ControlFlags\"].Value\n$nsd.Properties[\"Group\"].Value = $sd.Descriptor.Properties[\"Group\"].Value\n$nsd.Properties[\"Owner\"].Value = $sd.Descriptor.Properties[\"Owner\"].Value\n$nsd.Properties[\"SACL\"].Value = $sd.Descriptor.Properties[\"SACL\"].Value\n$nsd.Properties[\"TIME_CREATED\"].Value = $sd.Descriptor.Properties[\"TIME_CREATED\"].Value\n\nfor($i=0; $i -lt $sd.Descriptor.DACL.Count; $i++)\n{\n    $nsd.DACL += $sd.Descriptor.DACL[$i]\n}\n\n$nsd.DACL += $ACE\n# List all users having access\n$nsd.DACL | ForEach-Object {$_.Trustee.Name}\n\n$s.SetSecurityDescriptor($nsd) #Should return 0\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In a hurry to write this post, so will add explanation later Recently a fellow colleague asked\u00a0me to help him in doing this in PowerShell, as usual I went to google, much to my disappointment I couldn&#8217;t find a single solution anywhere on web (TechNet, Script Center, Powershell,com etc.) So, I decided to sit in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,221,281],"tags":[],"_links":{"self":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts\/176"}],"collection":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=176"}],"version-history":[{"count":0,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts\/176\/revisions"}],"wp:attachment":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}