PowerShell Example via WinRM

Rotate an IIS Application Pool Credential via WinRM

Prerequisites

The target server must be running Windows Server 2012 and above and have the IISAdministration module installed and enabled.

Using Admin Credentials

To update the 'Log On As' property on a IIS Application Pool, you will need a credential with the appropriate permissions, such as an Administrator account.

When attaching a PAM script to a record, you have the option to add a Resource Credential that is passed to the Gateway as part of the BASE64-encoded JSON data. The above credential will need to be attached as a Resource Credential.

As many Resource Credentials can be attached to a PAM script, knowing the UID of the Resource Credential you have attached helps ensure your script uses the correct one to update the Service's 'Log On As' property.

Updating the IIS App Pool

Using the IISServerManager, you can update the credentials and restart the ISS App Pool by invoking the script block below.

$IISAppPoolName = '<App Pool Name>'
$AppPoolStatus = Invoke-Command -Computername '<Target Machine>' -Credential $AdminCredential -ScriptBlock { 
    Import-Module IISAdministration 
    $ServerManager = Get-IISServerManager
    $AppPool = Get-IISAppPool -Name $Using:IISAppPoolName
    $AppPool.Stop()
    $AppPool.ProcessModel.IdentityType = "SpecificUser"
    $AppPool.ProcessModel.Username = $Using:Params.user
    $AppPool.ProcessModel.Password = $Using:Params.newPassword
    $ServerManager.CommitChanges()
    $AppPool.Start()
    return $AppPool
}

Write-Debug "$IISAppPoolName is: $($AppPoolStatus.State)"

Full Example

[CmdletBinding()]
param (
    [Parameter(ValueFromPipeline=$true)]
    [string]
    $B64Input
)

$ErrorActionPreference = "Stop"
$DebugPreference = 'Continue'

function ConvertFrom-B64 {
    param (
        [string] $B64String
    )

    try {
        $Json = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($B64String))
        $Output = $Json | ConvertFrom-Json
    }
    catch {
        Write-Error "Failed to convert Base64 string: $B64String"
    }
    return $Output
}

# The JSON data is passed to the Gateway as a Base64 encoded string.
$Params = ConvertFrom-B64 -B64String $B64Input
Write-Debug "Running Post-Rotation Script on: $($Params.userRecordUid)" 

# Convert the attached Resource Records from Base64 encoded JSON string and find the 
# Admin Record we need to update the Service's `Log On As` property by filtering by the 
# Admin Record's UID.
$ResourceCredentials = ConvertFrom-B64 -B64 $Params.records
$AdminRecord = $ResourceCredentials | Where-Object { $_.uid -eq '<Admin Record UID>' }

# Each record type will have a different JSON structure. In this instance, we are using 
# a PAM Directory record type, so we need to build the username from the `login` and 
# `domainName` properties.
try {
    $SecurePassword = ConvertTo-SecureString $AdminRecord.password -AsPlainText -Force
    $AdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "$($AdminRecord.login)@$($AdminRecord.domainName)", $SecurePassword
    Write-Debug "New PSCredential created for: $($AdminRecord.login)" 
}
catch {
    Write-Error "Failed to create PSCredential for: $($AdminRecord.login)"
}

$IISAppPoolName = '<App Pool Name>'
$AppPoolStatus = Invoke-Command -Computername '<Target Machine>' -Credential $AdminCredential -ScriptBlock { 
    Import-Module IISAdministration 
    $ServerManager = Get-IISServerManager
    $AppPool = Get-IISAppPool -Name $Using:IISAppPoolName
    $AppPool.Stop()
    $AppPool.ProcessModel.IdentityType = "SpecificUser"
    $AppPool.ProcessModel.Username = $Using:Params.user
    $AppPool.ProcessModel.Password = $Using:Params.newPassword
    $ServerManager.CommitChanges()
    $AppPool.Start()
    return $AppPool
}

Write-Debug "$IISAppPoolName is: $($AppPoolStatus.State)"

Last updated