Different results on a command run as job and regularly

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
drpiet
Posts: 15
Joined: Fri Jun 09, 2017 6:46 am

Different results on a command run as job and regularly

Post by drpiet » Fri Jul 13, 2018 12:55 pm

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?

User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jul 13, 2018 1:15 pm

What do you mean you can't "expand" properties?

User avatar
drpiet
Posts: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Mon Jul 16, 2018 7:33 am

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

User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Jul 16, 2018 10:04 am

You cannot expand an object property with quotation marks like that. Use the following syntax:

"$($MyVariable.NetworkProfile)"

User avatar
drpiet
Posts: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Mon Jul 16, 2018 12:02 pm

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).

User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Jul 16, 2018 12:09 pm

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

User avatar
drpiet
Posts: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Mon Jul 16, 2018 12:18 pm

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.

User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Jul 16, 2018 12:56 pm

Remove this: "-ErrorAction Stop" and try again. Check for errors.

User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Jul 16, 2018 1:19 pm

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'
}


User avatar
jvierra
Posts: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Jul 16, 2018 1:32 pm

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

Locked