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
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 »

This would be a better way to do this:

Code: Select all

$deploy = {
    Param (
        $path,
        $ResourceGroup,
        $VMName
    )
    Try{
        Import-AzureRmContext -Path $path  -ErrorAction Stop | out-null
        Get-AzureRMVm -Name $vmname -ResourceGroupName $ResourceGroup -ErrorAction Stop
    }
    Catch{
        Throw $_ 
    }
}
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 »

jvierra wrote: 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'
}

The code have an error, it say $myvariable.networkprofile when it should say $allvms.networkprofile.
In any case, the code is basically the same I'm using just with a try and catch, so the result is exactly the same.
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 »

Post the exact code you have run.

Are you running this at a PS prompt or in the ISE?
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 »

The exact code is on the original post, that is what I'm running.
And I'm running it on the prompt.
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 »

Then you haven't run the code I posted. I know your original code does not work. That is why I posted an upgrade.
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 »

Don't take me wrong, I appreciate your answers and your time.
But the code you posted the only thing that does different is running the command inside a try/catch structure, so I know that will not change the result. For sure is a better way to code, but is not what I'm looking for right now.
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 »

Have you tried it yet? You are making assumptions about what the code does and how it works without even trying it.
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 appreciate your help, but there is absolutely nothing to try. This is not based on assumptions, just powershell knowledge.
Thanks
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 »

Remoting does not return full objects.. It returns serialized objects. Jobs further return objects that are serialized.

Perhaps you should post I UserVoice to get others to address this issue. I cannot reproduce 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 »

Here is the forum for Azure scripting. A more comprehensive answer to you issue can be had as this forum supports the AzureRM module and its use.

Azure Scripting and Command Line Tools
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