I want to check the status of last command and based on the exit code, the commands will be executed further.
The last command execute was:
$hiveJob
You're talking about "exit code". If you mean $LastExitCode
automatic variable, it is only populated when you call windows program, RAR for example:
$x=rar
$LastExitCode
It will return exit code 7 (if you have RAR installed).
cmdlets, however, don't fill this variable. You can use another automatic variable $?
for this:
$x=gci
$?
It only gives $True
if command completed successfully or $False
if there was an error.
From Get-Help about_If:
Syntax The following example shows the If statement syntax:
if (<test1>)
{<statement list 1>}
[elseif (<test2>)
{<statement list 2>}]
[else
{<statement list 3>}]
Note: the square brackets around the elseif and else indicate they are optional.
Assign your returned object to a variable:
$hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition $hiveJobDefinition
Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 5400
$Result = Get-AzureHDInsightJobOutput -Cluster $clusterName -JobId $hiveJob.JobId -StandardOutput
Then
if ($Result.ExitCode -eq 0)
{
#More commands
}