Different results on a command run as job and regularly

Ask your 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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
drpiet
Posts: 22
Last visit: Thu Oct 06, 2022 11:07 am

Different results on a command run as job and regularly

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

What do you mean you can't "expand" properties?
User avatar
drpiet
Posts: 22
Last visit: Thu Oct 06, 2022 11:07 am

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

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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

"$($MyVariable.NetworkProfile)"
User avatar
drpiet
Posts: 22
Last visit: Thu Oct 06, 2022 11:07 am

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

Post 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).
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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
User avatar
drpiet
Posts: 22
Last visit: Thu Oct 06, 2022 11:07 am

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

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Remove this: "-ErrorAction Stop" and try again. Check for errors.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

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

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked