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
jvierra
Posts: 13175
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 2:00 pm

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: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Wed Jul 18, 2018 6:02 am

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.

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

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

Post by jvierra » Wed Jul 18, 2018 1:18 pm

Post the exact code you have run.

Are you running this at a PS prompt or in the ISE?

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 » Fri Jul 27, 2018 6:04 am

The exact code is on the original post, that is what I'm running.
And I'm running it on the prompt.

User avatar
jvierra
Posts: 13175
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 27, 2018 6:09 am

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: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Fri Jul 27, 2018 10:12 am

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.

User avatar
jvierra
Posts: 13175
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 27, 2018 10:52 am

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: 15
Joined: Fri Jun 09, 2017 6:46 am

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

Post by drpiet » Fri Jul 27, 2018 11:18 am

I appreciate your help, but there is absolutely nothing to try. This is not based on assumptions, just powershell knowledge.
Thanks

User avatar
jvierra
Posts: 13175
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 27, 2018 11:26 am

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.

User avatar
jvierra
Posts: 13175
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 27, 2018 11:55 am

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

Locked