Set-NTFSOwner doesn't work correctly with service account

Timo Riikonen 1 Reputation point
2020-09-14T09:14:39.163+00:00

I have Powershell script that creates home directory:

New-Item -Path \\fileserver\homedir$\acn -ItemType Directory -Force -ErrorAction Stop | Out-Null  

When I test this with my own account that is Domain Admin, everything works fine.
But when I try to use service account to do the same, it behaves differently. I have added service account as Domain Admin (at least for the first test round).

First difference (please verify):
With my account the owner becomes acn.
With service account the owner becomes the service account.

Then next command is:

Set-NTFSOwner -Path $HomeDir -Account 'Administrators' | Out-File "test.log"  

With my account owner changes as the admin group in the file server, just as it should.
With service account, nothing happens. Service account has full control access to this home directory and to the \fileserver\homedir$ directory.

Background info:

  • My own Powershell Module directory is empty
  • Powershell version:
    Major Minor Build Revision

4 0 30319 42000

  • Import-modules
    Import-Module activedirectory -ErrorAction Stop | Out-Null  
    Import-Module ntfssecurity -ErrorAction Stop | Out-Null  
    
Windows 10 Security
Windows 10 Security
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
2,769 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 32,081 Reputation points
    2020-09-14T14:07:43.29+00:00

    I am running Powershell version 5.1.19041.1 on Windows 10 2004. There is no Set-NTFSOwner command. After some research I did find a 3rd party NTFSSecurity module here.

    https://ntfssecurity.readthedocs.io/en/latest/Cmdlets/Set-NTFSOwner/

    Is that module that you are using? If so, you should contact the author for support.

    Is there some special requirement that you need to set the file owner? Why don't you just grant access to the user?

    icacls.exe $HomeDir /grant "$($user):(OI)(CI)(F)" | Out-File "test.log" 
    

    If you just need to set the owner to the Administrators group, you can use:

    takeown.exe /a $HomeDir  | Out-File "test.log"
    
    0 comments No comments

  2. Rich Matheisen 45,096 Reputation points
    2020-09-14T14:16:52.407+00:00

    When (or if) that Set-NTFSOwner fails you'll miss the error in the file "test.log" because the error is sent on stream #2 (the Error stream). You might try something like this to see if there's an exception (maybe a non-terminating one) being thrown:

    Try{  
        Set-NTFSOwner -Path $HomeDir -Account 'Administrators' -ErrorAction STOP  
    }  
    Catch{  
        $_ | Out-File "test.log"  
    }  
    

    . . . or redirect the stream to the "Success stream" by using "2>>&1 'test.log' instead of using "| Out-File 'test.log'.

    about_redirection

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,386 Reputation points Microsoft Vendor
    2020-09-15T02:54:09.9+00:00

    Hi,

    As it's a third party module you could contact the author. I find the NTFSSecurity project in Github
    https://github.com/raandree/NTFSSecurity/issues

    Best Regards,
    Ian

    0 comments No comments