65.9K
CodeProject 正在变化。 阅读更多。
Home

重置 IIS 服务器应用程序池的用户名和密码

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2018年8月14日

CPOL
viewsIcon

10722

如何重置 IIS 服务器应用程序池的用户名和密码

有时我们需要获取应用程序池的用户名和密码,以便进行进一步的修改,例如刷新帐户或更改用户密码。

以下 PowerShell 脚本将循环遍历所有应用程序池(不包括默认池),并使用相同的用户名和密码重置它们。 脚本可以轻松定制,以便在发生更改时更新密码。

# Check if appcmd.exe exists in default path
if (Test-Path  ("c:\windows\system32\inetsrv\appcmd.exe")) {    

	# Set AppCmd.exe path in variable for further usage.
    $AppCmdPath = 'c:\windows\system32\inetsrv\appcmd.exe'
	
    # Get list of application pools
    & $AppCmdPath list apppools /text:name | 
        ForEach-Object { 
                    
        #Get application pool name
        $PoolName = $_
        
        #Exclude inbuild Application Pools
        if(
        $PoolName -eq "DefaultAppPool" -Or 
        $PoolName -eq "Classic .NET AppPool" -Or 
        $PoolName -eq ".NET v2.0 Classic"-Or
        $PoolName -eq ".NET v2.0"-Or 
        $PoolName -eq ".NET v4.5 Classic" -Or
        $PoolName -eq ".NET v4.5"
        ){
            Write-Host  "Inbuild Pool" + $PoolName
        }
        else{
            #Get username                   
            $PoolUserCmd = $AppCmdPath + ' list apppool "' + 
                           $PoolName + '" /text:processmodel.username'
            $PoolUser = invoke-expression $PoolUserCmd 
                                          
            #Get password
            $PoolPasswordCmd = $AppCmdPath + ' list apppool "' + 
                               $PoolName + '" /text:processmodel.password'
            $PoolPassword = invoke-expression $PoolPasswordCmd 

            #Check if credentials exists
            if ($PoolPassword -ne "") {
                           
                #Re-set the app pool with the same credentials             
                & $AppCmdPath set config -section:system.applicationHost/applicationPools 
                "/[name='$($PoolName )'].processModel.identityType:SpecificUser" 
                "/[name='$($PoolName)'].processModel.userName:$($PoolUser)" 
                "/[name='$($PoolName)'].processModel.password:$($PoolPassword)" 
            }
        }        
    }
    # Do IISRESET after re-setting passwords
    & {iisreset}   
}
else {
    Write-Host -Object 'Could not find the appcmd.exe path at default location, 
                        please try at different place.'
}

请注意,PowerShell 脚本应以管理员权限执行。

© . All rights reserved.