2
votes

I am deploying a web site using power script, server is running on windows 7 with IIS 7.5
I am using following code to deploy the application
$arguments = [string[]]@( "-verb:sync", "-source:package='$PackagePath'", "-dest:auto,computerName='$PublishUrl',AuthType='NTLM'", "-setParam:name='IIS Web Application Name',value='$($WebApp)'", "-allowUntrusted")
Start-Process $msdeploy -ArgumentList $arguments -NoNewWindow -Wait

above script is working fine.

I want to catch the negative case.When I disable the web deploy agent service on the machine,it throws a message (Power shell is not treating this as error or exception)

Error Code: ERROR_COULD_NOT_CONNECT_TO_REMOTESVC
More Information: Could not connect to the remote computer ("localhost") using the specified process ("Web Deployment Agent Service") because the server did not respond. Make sure that the process ("Web Deployment Agent Service") is start ed on the remote computer. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_COULD_NOT_CONNECT_TO_REMO TESVC.
Error: The remote server returned an error: (503) Server Unavailable.
Error count: 1.

How can I catch this in powershell.

2

2 Answers

1
votes

I solved this problem with the following command:

$arguments = [string[]]@(
        "-verb:sync",
        "-source:package='$PackagePath'",
        "-dest:auto,computerName='$PublishUrl',AuthType='NTLM'",
         "-setParam:name='IIS Web Application Name',value='$($WebApp)'",
        "-allowUntrusted")

    #Restore IIS
    $proc = Start-Process $msdeploy -ArgumentList $arguments -NoNewWindow -Wait -PassThru
    $handle = $proc.Handle
    $proc.WaitForExit();

    if ($proc.ExitCode -ne 0) {
        Write-Warning "$_ exited with status code $($proc.ExitCode)"
    }

The secret here is the "-PassThru". Check out microsoft documentation.

0
votes

Try running this at the end of your script:

if ($Error.Count -gt 0)
{
    $Error | ForEach-Object { Write-Host $_ -ForegroundColor Red }
    exit -1;
}