locked
Setting remote NTFS permissions using foreach in PowerShell RRS feed

  • Question

  • Hi all,

    Can someone provide some guidance here. Below is my PowerShell script that I want to use to set NTFS permissions remotely on numerous servers. The bit I am hung up on is how to successfully set the <servername> variable. My first thoughts were just to set the variable in the unc path as $server but when the ps1 is run it returns the following format issue:

    Get-ACL : Cannot find path '\\@{Hostname=serverfqdn}\D$\Folder' because it does not exist. That is, PS interprets my $server as @{Hostname=serverfqdn. How do I remove the @{Hostname= component?  My script here:

    $servers = Import-Csv -Path "\\testserver\folder\Physical_servers.csv"

    foreach($server in $servers){

    $colRights = [System.Security.AccessControl.FileSystemRights]"Modify"
     
    $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
    $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
     
    $objType =[System.Security.AccessControl.AccessControlType]::Allow
     
    $objUser = New-Object System.Security.Principal.NTAccount("domain\group}")
     
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)

    $objACL = Get-ACL "\\$Server\D$\Folder"
    $objACL.AddAccessRule($objACE)
     
    Set-ACL "\\$Server\D$\Folder" $objACL
    }
    • Edited by Qbic808 Thursday, November 30, 2017 5:17 PM
    Thursday, November 30, 2017 5:01 PM

Answers

  • Instead of "\\$Server.Name\D$\Folder", use "\\$($Server.Hostname)\D$\Folder"

    Seeing as Hostname is the column that the name is located.


    Jeremy Corbello | https://www.jeremycorbello.com



    • Edited by Jacorbello Friday, December 1, 2017 2:22 PM
    • Marked as answer by Qbic808 Tuesday, December 5, 2017 10:32 AM
    Friday, December 1, 2017 2:21 PM
  • see the difference of 2 command's result using Write-host in Powershell console, you will get to know what was wrong.

    write-host "\\$server.name\d$\folder"

    and

    write-host "\\$($server.name)\d$\folder"


    • Edited by asharma5 Friday, December 1, 2017 2:29 PM
    • Proposed as answer by jrv Friday, December 1, 2017 6:15 PM
    • Marked as answer by Qbic808 Tuesday, December 5, 2017 10:31 AM
    Friday, December 1, 2017 2:29 PM

All replies

  • You have a CSV which needs to be addressed with the server name.

    $server.Name if that is the columns name.


    \_(ツ)_/

    Thursday, November 30, 2017 8:46 PM
  • Thanks for you help here jrv. I believe I've tried what you suggested.

    That is:

    $objACL = Get-ACL "\\$Server.Name\D$\Folder

    and  

    Set-ACL "\\$Server.Name\D$\Folder" $objACL

    Unfortunately I get the same issue....

    Get-ACL : Cannot find path '\\@{Hostname=fqdn-of-server}.Name\D$\folder' because it does not exist. Any other ideas?


    • Edited by Qbic808 Friday, December 1, 2017 1:53 PM
    Friday, December 1, 2017 1:53 PM
  • Instead of "\\$Server.Name\D$\Folder", use "\\$($Server.Hostname)\D$\Folder"

    Seeing as Hostname is the column that the name is located.


    Jeremy Corbello | https://www.jeremycorbello.com



    • Edited by Jacorbello Friday, December 1, 2017 2:22 PM
    • Marked as answer by Qbic808 Tuesday, December 5, 2017 10:32 AM
    Friday, December 1, 2017 2:21 PM
  • see the difference of 2 command's result using Write-host in Powershell console, you will get to know what was wrong.

    write-host "\\$server.name\d$\folder"

    and

    write-host "\\$($server.name)\d$\folder"


    • Edited by asharma5 Friday, December 1, 2017 2:29 PM
    • Proposed as answer by jrv Friday, December 1, 2017 6:15 PM
    • Marked as answer by Qbic808 Tuesday, December 5, 2017 10:31 AM
    Friday, December 1, 2017 2:29 PM
  • Thanks for this - useful to understand difference and how to check
    Tuesday, December 5, 2017 10:33 AM
  • Thanks Jeremy - this has been really helpful.
    Tuesday, December 5, 2017 10:34 AM