Page 1 of 3

Different results on a command run as job and regularly

Posted: Fri Jul 13, 2018 12:55 pm
by drpiet
Hello everybody.
This is about running an "azure command" as a background job.
I've been dealing with this for a week already.

If I run this command:

Code: Select all

Get-AzureRMVm | where{($_.ResourceGroupName -eq $ResourceGroup) -and ($_.name -eq $vmname)} -ErrorAction Stop
Everything works just fine, I receive the information about my VM and I can expand all the properties on it.
But when I run it like this:

Code: Select all

$path = "\profile.json"
$deploy = {
    Param ($path,
    $ResourceGroup,
    $VMName)
Import-AzureRmContext -Path $path | out-null
Get-AzureRMVm | where{($_.ResourceGroupName -eq $ResourceGroup) -and ($_.name -eq $vmname)} -ErrorAction Stop
}
Save-AzureRmProfile -Path $path -force
$job = Start-Job -ScriptBlock $deploy -ArgumentList $path, $ResourceGroup, $VMName
Wait-Job $job
$AllVMs = Receive-Job $job
I receive the information about the VM, but I can’t expand the properties on it.
Anybody have any idea why and most importantly, how to solve it?

Re: Different results on a command run as job and regularly

Posted: Fri Jul 13, 2018 1:15 pm
by jvierra
What do you mean you can't "expand" properties?

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 7:33 am
by drpiet
By saving the result of the command on a variable, I would be able to query for example "$MyVariable.NetworkProfile", which will return the ID of the network interface and if I keep digging I might get some more information.
When the same command is ran as job, I will not be able to query (following with the same example) "$MyVariable.NetworkProfile".

Diego

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 10:04 am
by jvierra
You cannot expand an object property with quotation marks like that. Use the following syntax:

"$($MyVariable.NetworkProfile)"

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 12:02 pm
by drpiet
I'm not using quotes on my code, I used them to show you what I meant by expanding properties.
The point of this entire thing is, this code:

Code: Select all

$MyVariable.NetworkProfile
return one value when the command is run directly and a different value when ran as a job (exactly the same code).

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 12:09 pm
by jvierra
The most likely explanation is that you are returning more than one object.

This will tell you how many objects are returned:

Write-Host $MyVariable.Count

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 12:18 pm
by drpiet
It is just one object.
I'm running a get-azurermvm and I'm passing resourcegroup and VM name as parameter, so there is no way to return to return multiple objects.

Besides that, we are talking about exactly the same command, against the same environment. The only difference is: the first case is "interactive", the second one is "as a background job".

It has to be some limitation when running a command as a background job. But couldn't find any documentation about it.

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 12:56 pm
by jvierra
Remove this: "-ErrorAction Stop" and try again. Check for errors.

Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 1:19 pm
by jvierra
I would do it like this to hedge against errors.

Code: Select all

$path = '.\profile.json'
$deploy = {
    Param (
        $path,
        $ResourceGroup,
        $VMName
    )
    Try{
        Import-AzureRmContext -Path $path  -ErrorAction Stop | out-null
        Get-AzureRMVm -ErrorAction Stop| 
            where{
                $_.ResourceGroupName -eq $ResourceGroup -and
                $_.name -eq $vmname
            }
    }
    Catch{
        Throw $_ 
    }
}

Save-AzureRmProfile -Path $path -force
$job = Start-Job -ScriptBlock $deploy -ArgumentList $path, $ResourceGroup, $VMName
Wait-Job $job
Write-Host $job.Status # check for error
if($AllVMs = Receive-Job $job){
    $MyVariable.NetworkProfile
}else{
    Write-Host 'No data returned'
}


Re: Different results on a command run as job and regularly

Posted: Mon Jul 16, 2018 1:32 pm
by jvierra
Please read the following document on how to use Azure CmdLets in a job and how to use the context across sessions:

https://docs.microsoft.com/en-us/powers ... rmps-6.4.0