Add IIS AppPool\ASP.NET v4.0 to local windows group

巧了我就是萌 提交于 2020-01-01 09:05:09

问题


I'm trying to script with PowerShell the act of adding the user IIS AppPool\ASP.NET v4.0 to the Performance Monitor Users group, to be able to use custom performance counters from an ASP.NET application. But, I can't figure out how to address the automatically created ASP.NET user using ADSI.

This works for me:

 $computer = $env:COMPUTERNAME;

 $user = [ADSI]"WinNT://$computer/Administrator,user" 
 $groupToAddTo = "TestGroup"

 $parent = [ADSI]"WinNT://$computer/$groupToAddTo,group" 
 $parent.Add($user.Path)

However, I can't figure out how to find the ASP.NET v4.0 user:

 $computer = $env:COMPUTERNAME;
 # $user = [ADSI]"WinNT://$computer/IIS AppPool/ASP.NET v4.0,user" # <-- Doesn't work

 $groupToAddTo = "TestGroup"

 $parent = [ADSI]"WinNT://$computer/$groupToAddTo,group" 
 $parent.Add($user.Path)

Any clues on how to address that user using ADSI? Or, any other brilliant ways to achieve what I want using Powershell or other command-line tools? GUI works fine, however, automation is the key here.


回答1:


The following PowerShell script will add the application pool "ASP.NET v4.0" to the group "Performance Monitor Users"

$group = [ADSI]"WinNT://$Env:ComputerName/Performance Monitor Users,group"
$ntAccount = New-Object System.Security.Principal.NTAccount("IIS APPPOOL\ASP.NET v4.0")
$strSID = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier])
$user = [ADSI]"WinNT://$strSID"
$group.Add($user.Path)

Unlike the net localgroup command, this script will work fine with app pool names longer than 20 characters.



来源:https://stackoverflow.com/questions/18208890/add-iis-apppool-asp-net-v4-0-to-local-windows-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!