1

我正在使用后端 api 中的 winexe 在 Windows 域服务器上运行命令。我想将 IIS 应用程序池标识设置为 Active Directory 中的帐户。问题是在使用此命令时:

%windir%\system32\inetsrv\appcmd.exe set config /section:applicationPools ^
/[name='POOLNAME'].processModel.identityType:SpecificUser ^
/[name='POOLNAME'].processModel.userName:DOMAIN\USER ^
/[name='POOLNAME'].processModel.password:PASSWORD

即使用户名和密码不正确,它也每次都能成功运行。甚至池也以错误的密码启动。但是通过 GUI 设置错误的密码失败。

我想确定密码或用户名何时设置错误。

PS:我什至尝试Set-ItemProperty在 powershell 上使用,结果是一样的。

4

2 回答 2

1

您无法使用 AppPool 测试您的凭据,但您绝对可以测试它们。

# Service Principal credentials
$username = 'Username'
$password = 'Password' | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $username, $password


if (Test-Credential -Credential $credential) {
    Write-Verbose "Credentials for $($credential.UserName) are valid..."
    # do the appcmd stuff
}
else {
    Write-Warning 'Credentials are not valid or some other logic'
}

只需Test-Credential在脚本顶部添加函数定义

function Test-Credential {
    [CmdletBinding()]
    Param
    (
        # Specifies the user account credentials to use when performing this task.
        [Parameter()]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )
   
    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    $DS = $null
    $Username = $Credential.UserName
    $SplitUser = $Username.Split('\')
    if ($SplitUser.Count -eq 2 ) {$Username = $SplitUser[1]}
    
    if ($SplitUser.Count -eq 1 -or $SplitUser[0] -eq $env:COMPUTERNAME ) {
        $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine', $env:COMPUTERNAME)
    }
    else {
        try {
            $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
        }
        catch {
            return $false
        }
    }
        
    $DS.ValidateCredentials($Username, $Credential.GetNetworkCredential().Password)
   
}

(PS:代码是有效的,即使修饰符用反斜杠引用语法中断)

于 2020-07-11T19:10:16.823 回答
0

令人惊讶的是,我很困惑你可以这样做 - 但它仍然无法验证

appcmd set apppool junkapp /processmodel.password:junkpassword
于 2020-09-11T22:08:48.727 回答