如何通过Powershell为IIS APPPOOL \ *帐户添加ACL权限?

我希望能够为新网站设置IIS帐户具有修改权限。 我有以下脚本:

function Set-ModifyPermission ($directory, $username, $domain = 'IIS APPPOOL') { $inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit" $propagation = [system.security.accesscontrol.PropagationFlags]"None" $acl = Get-Acl $directory $user = New-Object System.Security.Principal.NTAccount($domain, $username ) $accessrule = New-Object system.security.AccessControl.FileSystemAccessRule($user, "Modify", $inherit, $propagation, "Allow") $acl.AddAccessRule($accessrule) set-acl -aclobject $acl $directory } 

但是,当我运行它,我得到这样的错误:

Set-Acl:此工作站与主域之间的信任关系失败。

我认为这是因为IIS APPPOOL不是一个真正的域,而是一个假的帐户上的一个奇怪的前缀。 有没有正确的方法来引用该帐户,以便我可以做这个工作?

首先,像这样使用Set-Acl,因为目录path是第一个位置参数:

 Set-Acl $directory $acl 

其次,你应该只用一个参数来创build用户对象:

 $user = New-Object System.Security.Principal.NTAccount("$domain\\$username") 

更新:似乎不会接受“IIS APPPOOL \ AppPoolName”作为NTAccount标识符。 现在有两种方法可以完成你正在做的事情:

  1. 使用AppPoolIdentities SID创build一个新的SID对象,并将其转换为NTAccount,如下所示: http : //iformattable.blogspot.com/2007/12/convert-sid-to-ntaccount-with.html ,您应该可以像对待任何其他NTAccount对象一样对待它。 如果您仍然希望能够为真实账户传递域名/用户名,则可以使用一些简单的逻辑,如果用户名是“AweSomeAppPool”且域为空,则默认为AppPool SID。

  2. 使用PowerShell调用icacls.exe,并使用它来授予/撤销任何权限,像这样(第一次正常icaclsforms命令提示符,然后powershell,注意到差异):

    icacls.exe test.txt /grant "IIS AppPool\DefaultAppPool":(OI)(CI)M

    cmd /c icacls test.txt /grant "IIS AppPool\DefaultAppPool:(OI)(CI)M"

如果你select第二个选项,一定要先手动testing,我没有机会自己testing这些具体的例子,但它应该工作

像这样的东西应该为你做的伎俩。 它应该能够解决IIS APPPOOl \ Anything以及…

 function Set-AclOnPath { param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $Path, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $DomainAccount ) #Put whatever permission you want here $permission = $DomainAccount,"ReadAndExecute","Allow" $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission $acl = Get-Acl $Path $acl.SetAccessRule($accessRule) $acl | Set-Acl $Path } 

以下工作在Windows 2012中获取IIS站点的SID。 它需要使用WebAdministration的PowerShell模块的IIS提供程序 ,但本文指出它将在Windows 2008R2上运行。

 $appPoolName = 'MyAppPool' $appPoolSid = (Get-ItemProperty IIS:\AppPools\$appPool).applicationPoolSid $identifier = New-Object System.Security.Principal.SecurityIdentifier $appPoolSid $user = $identifier.Translate([System.Security.Principal.NTAccount]) 

以下在Windows 2012中为我工作,不能得到其他的例子工作:

 Import-Module WebAdministration $appPoolName='MyAppPool' $folderDirectory='C:\MyWebFolder' $appPoolSid = (Get-ItemProperty IIS:\AppPools\$appPoolName).applicationPoolSid Write-Output "App Pool User $appPoolSid" $identifier = New-Object System.Security.Principal.SecurityIdentifier $appPoolSid $user = $identifier.Translate([System.Security.Principal.NTAccount]) Write-Output "Translated User $user.Value" $acl = Get-Acl $folderDirectory $acl.SetAccessRuleProtection($True, $False) $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,”FullControl”, “ContainerInherit, ObjectInherit”, “None”, “Allow”) $acl.AddAccessRule($rule) $acl | set-acl -path $folderDirectory