Pages

Thursday, March 17, 2016

Updating Application Pool Settings using powershell on a server stack

This is a powershell script that allows you to execute remotely to update the Application Pool settings on a website. Here I am trying to update the .Net version to 4.0.

$environment = (Read-Host 'Which environment is this? (localhost, erpint, erpqa, staging, candidate, production)').toLower()
# check if user enter incorrect environment

for( $i = 1; $i -le 3; $i++ ){  #loop thru each node

$server = $null
Write-Host $environment

            $server = $environment + "wfe" + $i + ".pmienvs.pmihq.org"   #construct the server fqdn to remote connect. This will different for you

Write-Host "Remote Executing on $server ..."
       
        invoke-command -computername $server {
            Import-Module WebAdministration
            $iisAppPoolName = "yourwebsite.org"
            $iisAppPoolDotNetVersion = "v4.0"

            #navigate to the app pools root
            cd IIS:\AppPools\

            #check if the app pool exists
            if (!(Test-Path $iisAppPoolName -pathType container))
            {
                #App pool does not exist
                "App Pool $iisAppPoolName does not exist. Skipping updates."
            }
            else
            {
                #update the app pool settings
                $appPool = Get-Item $iisAppPoolName
                $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
                "App Pool $iisAppPoolName updated."
            }
        }
}

No comments: