jan-h
3/9/2015 - 10:59 AM

From http://www.abhinavrastogi.com/powershell_for_remote_tomcat_deployment.html

# Params:
# $tomcatUrl: Url of tomcat
# $warPath: Url of war file containing application
# $appVirtualPath: Virtual path of application to be deployed
# $user: User with permissions to deploy i.e. a user in tomcat-users.xml with manager-script role
# $password: Password for specified user
Function DeployToTomcat($tomcatUrl, $warPath, $appVirtualPath, $user, $password)
{
        # Webclient for making requests
        $webclient = new-object System.Net.WebClient

        # Object for keeping authorization credentials
        $credCache = new-object System.Net.CredentialCache

        # Tomcat uses basic auth by default
        $creds = new-object System.Net.NetworkCredential($user, $password)
        $credCache.Add($tomcatUrl + "manager/text/list", "Basic", $creds)
        $webclient.Credentials = $credCache

        # 1. Stop application
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/stop?path=/" + $appVirtualPath)
        write-output $webpage

        # 2. Undeploy application
        $webpage = $webclient.DownloadString($tomcatUrl + "manager/text/undeploy?path=/" + $appVirtualPath)
        write-output $webpage

        # 2A. Wait for application to be undeployed
        [System.Threading.Thread]::Sleep(10000)

        #3. Upload new application
        $uploadUrl = ($tomcatUrl  + "manager/text/deploy?path=/"  + $appVirtualPath)
        $bytes =  [System.IO.File]::ReadAllBytes($warPath)
        $uploadresults = $webclient.UploadData($uploadUrl, "PUT", $bytes)
        #write-output $uploadresults

        #3A. Wait for new application to be pushed to tomcat server and unzipped
        [System.Threading.Thread]::Sleep(10000)

        #4. Start new application
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/start?path=/" + $appVirtualPath)
        write-output $webpage

        #5. Get a list of running applications
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/list")
        write-output $webpage

        #6. Print url for application
        write-output ("Application running here: " + $tomcatUrl + $appVirtualPath)
}