Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RosenPetrov/3b52db21ac713227a8467f9d87f9bd24 to your computer and use it in GitHub Desktop.
Save RosenPetrov/3b52db21ac713227a8467f9d87f9bd24 to your computer and use it in GitHub Desktop.
PowerShell script for configuring Performance Counters access in Sitecore 9
function IISReset {
[CmdletBinding(SupportsShouldProcess = $true)]
Param
(
[string]$Reason,
[int]$TryNumber = 0,
[switch]$Force
)
if ($Force -or $PSCmdlet.ShouldProcess("IIS", $Reason)) {
$process = Start-Process "iisreset.exe" -NoNewWindow -Wait -PassThru
if (($process.ExitCode -gt 0) -and ($TryNumber -lt 3) ) {
Write-Warning "IIS Reset failed. Retrying..."
$newTryNumber = $TryNumber + 1
IISReset -Reason $Reason -TryNumber $newTryNumber -Force
}
}
}
function AddAppPoolUserToGroups {
[CmdletBinding()]
Param
(
[string[]]$AppPools,
[string[]]$Groups
)
$needIISReset = $false
foreach ($gr in $Groups) {
$group = [ADSI]"WinNT://$Env:ComputerName/$gr,group"
$members = $group.psbase.invoke("Members") | ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
foreach ($pool in $AppPools) {
if ($members -contains $pool) {
Write-Warning "'$pool' AppPool user exists in '$gr' group"
}
else {
Write-Output "Adding '$pool' AppPool user to '$gr' group..."
$ntAccount = New-Object System.Security.Principal.NTAccount("IIS APPPOOL\$pool")
$strSID = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier])
$user = [ADSI]"WinNT://$strSID"
$group.Add($user.Path)
$needIISReset = $true
Write-Output "Adding '$pool' AppPool user to '$gr' group done."
}
}
}
if ($needIISReset -eq $true) {
IISReset -Reason "Groups changes will take effect after IIS Reset. Do it now?" -Confirm
}
}
$pools = @("APP_POOL_NAME")
$groups = @("Performance Log Users", "Performance Monitor Users")
AddAppPoolUserToGroups -AppPools $pools -Groups $groups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment