proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

PoshRSJob/Azure Automation/Powershell Modules Issue #194

Closed nathanmjones closed 5 years ago

nathanmjones commented 5 years ago

Do you want to request a feature or report a bug? Bug What is the current behavior? Successfully used PoshRSJob in Azure Automation Powershell Runbooks until recently. Now, when I update the Powershell modules the Runbooks fail.

WORKS: PoshRSJob 1.7.4.4 Azure 1.0.3 Azure.Storage 1.0.3 AzureRM.Automation 1.0.3 AzureRM.Compute 1.2.1 AzureRM.Profile 1.0.3 AzureRM.Resources 1.0.3 AzureRM.Sql 1.0.3 AzureRM.Storage 1.0.3

FAILS: PoshRSJob 1.7.4.4 Azure 5.3.0 Azure.Storage 4.6.0 AzureRM.Automation 5.1.1 AzureRM.Compute 5.6.0 AzureRM.Profile 5.6.0 AzureRM.Resources 6.5.0 AzureRM.Sql 4.11.3 AzureRM.Storage 5.1.0

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem Setup azure automation account. do not update modules. install poshrsjob 1.7.4.4 create runbook & it will complete update azure modules in the automation account execute runbook again and it will fail

What is the expected behavior?

Which versions of Powershell and which OS are affected by this issue? Did this work in previous versions of our scripts? Yes

Please provide a code example showing the issue, if applicable:


$jobs = Get-AzureRmVM -ResourceGroupName SERVERS | 
    % { 
        Get-AzureRmVM -ResourceGroupName SERVERS -Name $_.Name -Status 
    } | 
    % { 
        if (-Not ($_.Statuses[1].DisplayStatus -like "*Running*") -AND ($_.Name -like "AzureServer01*")) { 
            $vm = $_
            Start-Sleep -Seconds 1
            Start-RSJob {               
                Start-AzureRmVM -ResourceGroupName SERVERS -Name ($using:vm).Name 
            }
        }
    }
    Start-Sleep -Seconds 1
$jobs | Wait-RSJob | Receive-RSJob
$jobs | Remove-RSJob
nathanmjones commented 5 years ago

sorry if my information is vague but i am not a developer; just an end user lol.. I just use your awesome module to stop start servers on a daily basis/scheduled. Really has helped over the last 2years+ but for some reason it will no longer work :( if you can give me assistance to gather more info for you then I will happily do so... thanks in advance for any assistance..

MVKozlov commented 5 years ago

try to run your command block by block... what command fail exactly ? is it exception or some command never return ? if it is exception what is it (copy/paste output here)

nathanmjones commented 5 years ago

There is no output...! :( The Azure Runbook actually completes successfully but there is no output to the screen other than a successful authentication to Azure and choosing the Azure subscription.

If I run it locally in Powershell ISE, the code I shared above just completes and I return to a prompt. I spoke with Microsoft support about it and they checked their backend and told me there is no command sent to to either start/stop the servers...

I have 20+ Azure automation accounts all using this PoshRSJob for almost 2 years! Once I update the Modules the whole thing just stops working!

I just ran it in Automation/Runbook Test mode, after successful authentication it starts to go through the resource group I specified and it outputs info about all the servers it found.. Hoever, I notice in the output that it only gets to server 023 when in fact it should continue to 045 then because 040 to 045 match my code it should start them!

So, why is it stopping at 023?

AzureQoSEvent: CommandName - Get-AzureRmVM; IsSuccess - True; Duration - 00:02:41.2213817; Exception - ; Finish sending metric. 9:07:53 AM - GetAzureVMCommand end processing. 9:07:53 AM - GetAzureVMCommand end processing.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;

public class V2UsingVariable
{
    public string Name;
    public string NewName;
    public object Value;
    public string NewVarName;
}

public class RSRunspacePool
{
    public System.Management.Automation.Runspaces.RunspacePool RunspacePool;
    public System.Management.Automation.Runspaces.RunspacePoolState State;
    public int AvailableJobs;
    public int MaxJobs;
    public DateTime LastActivity = DateTime.MinValue;
    public string RunspacePoolID;
    public bool CanDispose = false;
}
public class RSJob
{
    public string Name;
    public int ID;
    public System.Management.Automation.PSInvocationState State;
    public object InputObject;
    public string InstanceID;
    public object Handle;
    public object Runspace;
    public System.Management.Automation.PowerShell InnerJob;
    public System.Threading.ManualResetEvent Finished;
    public string Command;
    public System.Management.Automation.PSDataCollection<System.Management.Automation.ErrorRecord> Error;
    public System.Management.Automation.PSDataCollection<System.Management.Automation.VerboseRecord> Verbose;
    public System.Management.Automation.PSDataCollection<System.Management.Automation.DebugRecord> Debug;
    public System.Management.Automation.PSDataCollection<System.Management.Automation.WarningRecord> Warning;
    public System.Management.Automation.PSDataCollection<System.Management.Automation.ProgressRecord> Progress;
    public bool HasMoreData = true;
    public bool HasErrors;
    public object Output;
    public string RunspacePoolID;
    public bool Completed = false;
    public string Batch;
    #pragma warning disable 414
    private bool IsReceived = false;
    #pragma warning restore 414
}

Creating RS collections Creating routine to monitor RS jobs Creating routine to monitor Runspace Pools Exporting function 'Get-RSJob'. Exporting function 'Receive-RSJob'. Exporting function 'Remove-RSJob'. Exporting function 'Start-RSJob'. Exporting function 'Stop-RSJob'. Exporting function 'Wait-RSJob'. Exporting alias 'ssj'. Exporting alias 'gsj'. Exporting alias 'rsj'. Exporting alias 'rmsj'. Exporting alias 'spsj'. Exporting alias 'wsj'. ParameterSet: All Bound: False ParameterSet: Job ScriptBlock: ParameterSet: Job

nathanmjones commented 5 years ago

Actually, I just ran it again in Test mode and this time I did see references to the servers higher than 023! it is just not finding the servers in numerical order. But again I had the same output as above...

MVKozlov commented 5 years ago

I'm sorry, I do not have any azure servers to test, but you can add write-host 'something' to you script before and after any significant cmdlets to test where your script stuck or return nothing something like this

$jobs = Get-AzureRmVM -ResourceGroupName SERVERS | 
    % { 
        Write-Host "Getting AzureVM $($_.Name)"
        Get-AzureRmVM -ResourceGroupName SERVERS -Name $_.Name -Status 
    } | 
    % { 
        Write-Host "Processing AzureVM $($_.Name)"
        if (-Not ($_.Statuses[1].DisplayStatus -like "*Running*") -AND ($_.Name -like "AzureServer01*")) { 
            $vm = $_
        Write-Host "Will start AzureVM $($_.Name), DisplayStatus: $($_.Statuses[1].DisplayStatus)"
            Start-Sleep -Seconds 1
            Start-RSJob {               
                Start-AzureRmVM -ResourceGroupName SERVERS -Name ($using:vm).Name 
            }
        Write-Host "Started AzureVM $($_.Name)"
        }
        else {
            Write-Host "Skip processing AzureVM $($_.Name), DisplayStatus: $($_.Statuses[1].DisplayStatus)"
    }
    }
Start-Sleep -Seconds 1
Write-Host "There is $($jobs.Count) Jobs, Waiting"
$jobs | Wait-RSJob | Receive-RSJob
Write-Host "There is $($jobs.Count) Jobs, Removing"
$jobs | Remove-RSJob

And start it in environment that can show you what happen Or may be add start-transcriot before code and run it thru runbook

I suppose that something wrong with if $_.Statuses[1].DisplayStatus but not RSJob

nathanmjones commented 5 years ago

False Alarm, the issue seems to be with Get-AzureRMVM When you specify a -ResourceGroup it can only check the 1st 50 servers it finds, then it stops!! Deleted Azure Automation account, re-created new one which has older modules. everything working again. Thanks.