<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>powershell &amp;mdash; Kevin Sandy</title>
    <link>https://kevinsandy.com/tag:powershell</link>
    <description>Thoughts, musings, ramblings, and rants</description>
    <pubDate>Fri, 17 Apr 2026 07:50:19 +0000</pubDate>
    <image>
      <url>https://i.snap.as/IC0yYUyI.png</url>
      <title>powershell &amp;mdash; Kevin Sandy</title>
      <link>https://kevinsandy.com/tag:powershell</link>
    </image>
    <item>
      <title>Assigning Unix Attributes to Active Directory Objects</title>
      <link>https://kevinsandy.com/assigning-unix-attributes-to-active-directory-object?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[I run Active Directory to manage my users and groups. Most of my servers run Linux, and I also run a Synology DiskStation that serves files via NFS and CIFS. To keep file permissions and ownership consistent, I assign static UID and GID values to my Active Directory users and groups. Rather than manually assigning UID and GID values, I created a PowerShell script to do it for me.&#xA;&#xA;!--more--&#xA;&#xA;$objectBase = &#34;ou=Digital Lotus,dc=corp,dc=digitallotus,dc=com&#34;&#xA;$idRangeBase = 100000&#xA;$primaryGid = 101110&#xA;$loginShell = &#34;/bin/bash&#34;&#xA;$homeDirectoryBase = &#34;/users&#34;&#xA;&#xA;Get-ADObject `&#xA;        -LDAPFilter &#34;(&amp;(|(objectClass=user)(objectClass=group))(!objectClass=computer))&#34; `&#xA;        -SearchBase &#34;$objectBase&#34; `&#xA;        -Properties objectClass,objectSid,uidNumber,gidNumber,sAMAccountName,loginShell,unixHomeDirectory,primaryGroupID | ForEach {&#xA;        &#xA;    $sAMAccountName = $.sAMAccountName&#xA;    $objectRid = ($.objectSid -split &#34;-&#34;)[-1]&#xA;    $idNumber = $idRangeBase + $objectRid&#xA;&#xA;    if ( $.objectClass -eq &#34;user&#34; ) {&#xA;        if ( -not $.uidNumber ) {&#xA;            Write-Host &#34;Adding uidNumber $idNumber to $sAMAccountName&#34;&#xA;            $ | Set-ADObject -Add @{uidNumber=$idNumber}&#xA;        }&#xA;        if ( -not $.gidNumber ) {&#xA;            Write-Host &#34;Adding gidNumber $gidNumber to $sAMAccountName&#34;&#xA;            $ | Set-ADObject -Add @{gidNumber=$primaryGid }&#xA;        }&#xA;        if ( -not $.loginShell ) {&#xA;            Write-Host &#34;Adding loginShell $loginShell to $sAMAccountName&#34;&#xA;            $ | Set-ADObject -Add @{loginShell=$loginShell}&#xA;        }&#xA;        if ( -not $.unixHomeDirectory ) {&#xA;            $homeDirectory = &#34;$homeDirectoryBase/$sAMAccountName&#34;&#xA;            Write-Host &#34;Adding unixHomeDirectory $homeDirectory to $sAMAccountName&#34;&#xA;            $ | Set-ADObject -Add @{unixHomeDirectory=$homeDirectory}&#xA;        }&#xA;    }&#xA;&#xA;    if ( $.objectClass -eq &#34;group&#34; -and -not $.gidNumber ) {&#xA;        Write-Host &#34;Adding gidNumber $idNumber to $sAMAccountName&#34;&#xA;        $ | Set-ADObject -Add @{gidNumber=$idNumber}&#xA;    }&#xA;&#xA;}&#xA;&#xA;The objectBase variable is the base of the search for users and groups, and idRangeBase is the starting value for the IDs. The Active Directory object&#39;s relative ID is added to idRangeBase to create the actual UID or GID number.&#xA;&#xA;#activedirectory #powershell]]&gt;</description>
      <content:encoded><![CDATA[<p>I run Active Directory to manage my users and groups. Most of my servers run Linux, and I also run a Synology DiskStation that serves files via NFS and CIFS. To keep file permissions and ownership consistent, I assign static UID and GID values to my Active Directory users and groups. Rather than manually assigning UID and GID values, I created a PowerShell script to do it for me.</p>



<pre><code class="language-powershell">$objectBase = &#34;ou=Digital Lotus,dc=corp,dc=digitallotus,dc=com&#34;
$idRangeBase = 100000
$primaryGid = 101110
$loginShell = &#34;/bin/bash&#34;
$homeDirectoryBase = &#34;/users&#34;

Get-ADObject `
        -LDAPFilter &#34;(&amp;(|(objectClass=user)(objectClass=group))(!objectClass=computer))&#34; `
        -SearchBase &#34;$objectBase&#34; `
        -Properties objectClass,objectSid,uidNumber,gidNumber,sAMAccountName,loginShell,unixHomeDirectory,primaryGroupID | ForEach {
        
    $sAMAccountName = $_.sAMAccountName
    $objectRid = ($_.objectSid -split &#34;-&#34;)[-1]
    $idNumber = $idRangeBase + $objectRid

    if ( $_.objectClass -eq &#34;user&#34; ) {
        if ( -not $_.uidNumber ) {
            Write-Host &#34;Adding uidNumber $idNumber to $sAMAccountName&#34;
            $_ | Set-ADObject -Add @{uidNumber=$idNumber}
        }
        if ( -not $_.gidNumber ) {
            Write-Host &#34;Adding gidNumber $gidNumber to $sAMAccountName&#34;
            $_ | Set-ADObject -Add @{gidNumber=$primaryGid }
        }
        if ( -not $_.loginShell ) {
            Write-Host &#34;Adding loginShell $loginShell to $sAMAccountName&#34;
            $_ | Set-ADObject -Add @{loginShell=$loginShell}
        }
        if ( -not $_.unixHomeDirectory ) {
            $homeDirectory = &#34;$homeDirectoryBase/$sAMAccountName&#34;
            Write-Host &#34;Adding unixHomeDirectory $homeDirectory to $sAMAccountName&#34;
            $_ | Set-ADObject -Add @{unixHomeDirectory=$homeDirectory}
        }
    }

    if ( $_.objectClass -eq &#34;group&#34; -and -not $_.gidNumber ) {
        Write-Host &#34;Adding gidNumber $idNumber to $sAMAccountName&#34;
        $_ | Set-ADObject -Add @{gidNumber=$idNumber}
    }

}
</code></pre>

<p>The <code>objectBase</code> variable is the base of the search for users and groups, and <code>idRangeBase</code> is the starting value for the IDs. The Active Directory object&#39;s relative ID is added to <code>idRangeBase</code> to create the actual UID or GID number.</p>

<p><a href="https://kevinsandy.com/tag:activedirectory" class="hashtag"><span>#</span><span class="p-category">activedirectory</span></a> <a href="https://kevinsandy.com/tag:powershell" class="hashtag"><span>#</span><span class="p-category">powershell</span></a></p>
]]></content:encoded>
      <guid>https://kevinsandy.com/assigning-unix-attributes-to-active-directory-object</guid>
      <pubDate>Sun, 27 Nov 2022 15:29:05 +0000</pubDate>
    </item>
  </channel>
</rss>