@@ -26,62 +26,110 @@ public class WaitAzureBackupJob : AzureBackupVaultCmdletBase
26
26
{
27
27
[ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . WaitJobFilterJobIdHelpMessage , ValueFromPipeline = true ) ]
28
28
[ ValidateNotNull ]
29
- public List < string > JobID { get ; set ; }
29
+ public object JobID { get ; set ; }
30
30
31
31
[ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . WaitJobFilterJobHelpMessage , ValueFromPipeline = true ) ]
32
32
[ ValidateNotNullOrEmpty ]
33
- public List < AzureBackupJob > Job { get ; set ; }
33
+ public object Job { get ; set ; }
34
34
35
35
[ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . WaitJobFilterTimeoutHelpMessage ) ]
36
36
[ ValidateRange ( 1 , Int64 . MaxValue ) ]
37
37
public long ? TimeOut { get ; set ; }
38
38
39
39
public override void ExecuteCmdlet ( )
40
40
{
41
- // ValidateNotNullNotEmpty makes sure that there exists at least one element
42
- this . ResourceGroupName = Job [ 0 ] . ResourceGroupName ;
43
- this . ResourceName = Job [ 0 ] . ResourceName ;
41
+ List < string > specifiedJobs = new List < string > ( ) ;
44
42
45
- base . ExecuteCmdlet ( ) ;
46
-
47
- ExecutionBlock ( ( ) =>
43
+ if ( Job != null )
48
44
{
49
- List < string > specifiedJobs = new List < string > ( ) ;
45
+ WriteDebug ( "Job is Powershell: " + ( Job is PSObject ) ) ;
46
+ WriteDebug ( "Job type: " + Job . GetType ( ) ) ;
50
47
51
- if ( ! TimeOut . HasValue )
48
+ if ( ( Job is PSObject ) && ( ( ( PSObject ) Job ) . ImmediateBaseObject is List < AzureBackupJob > ) )
52
49
{
53
- TimeOut = new long ( ) ;
54
- TimeOut = Int64 . MaxValue ;
50
+ WriteDebug ( "Type of input paramter is List<AzureBackupJob>" ) ;
51
+ foreach ( AzureBackupJob jobToWait in ( ( Job as PSObject ) . ImmediateBaseObject as List < AzureBackupJob > ) )
52
+ {
53
+ this . ResourceGroupName = jobToWait . ResourceGroupName ;
54
+ this . ResourceName = jobToWait . ResourceName ;
55
+ this . Location = jobToWait . Location ;
56
+
57
+ specifiedJobs . Add ( jobToWait . InstanceId ) ;
58
+ }
55
59
}
60
+ else if ( Job is List < AzureBackupJob > )
61
+ {
62
+ WriteDebug ( "Type of input paramter is List<AzureBackupJob> second case" ) ;
63
+ foreach ( AzureBackupJob jobToWait in ( Job as List < AzureBackupJob > ) )
64
+ {
65
+ this . ResourceGroupName = jobToWait . ResourceGroupName ;
66
+ this . ResourceName = jobToWait . ResourceName ;
67
+ this . Location = jobToWait . Location ;
56
68
57
- //if (JobID != null && Job != null)
58
- //{
59
- // foreach(string job in JobID)
60
- // {
61
- // WriteDebug("JobID: " + job);
62
- // }
63
- // foreach (AzureBackupJob job in Job)
64
- // {
65
- // WriteDebug("Job: " + job.InstanceId);
66
- // }
67
- // throw new Exception("Please use either JobID filter or Job filter but not both.");
68
- //}
69
-
70
- if ( Job != null )
69
+ specifiedJobs . Add ( jobToWait . InstanceId ) ;
70
+ }
71
+ }
72
+ else if ( ( Job is PSObject ) && ( ( ( PSObject ) Job ) . ImmediateBaseObject is AzureBackupJob ) )
71
73
{
72
- foreach ( AzureBackupJob inpJob in Job )
74
+ AzureBackupJob azureJob = ( ( Job as PSObject ) . ImmediateBaseObject as AzureBackupJob ) ;
75
+ this . ResourceGroupName = azureJob . ResourceGroupName ;
76
+ this . ResourceName = azureJob . ResourceName ;
77
+ this . Location = azureJob . Location ;
78
+
79
+ specifiedJobs . Add ( azureJob . InstanceId ) ;
80
+ }
81
+ else if ( Job is AzureBackupJob )
82
+ {
83
+ this . ResourceName = ( Job as AzureBackupJob ) . ResourceName ;
84
+ this . ResourceGroupName = ( Job as AzureBackupJob ) . ResourceGroupName ;
85
+ this . Location = ( Job as AzureBackupJob ) . Location ;
86
+
87
+ specifiedJobs . Add ( ( Job as AzureBackupJob ) . InstanceId ) ;
88
+ }
89
+ }
90
+ else
91
+ {
92
+ if ( ( JobID is PSObject ) && ( ( ( PSObject ) JobID ) . ImmediateBaseObject is List < string > ) )
93
+ {
94
+ foreach ( string idOfJobToWait in ( ( JobID as PSObject ) . ImmediateBaseObject as List < string > ) )
73
95
{
74
- specifiedJobs . Add ( inpJob . InstanceId ) ;
96
+ WriteDebug ( "Type of JobID filter is List<string>." ) ;
97
+ specifiedJobs . Add ( idOfJobToWait ) ;
75
98
}
76
99
}
77
- else
100
+ else if ( JobID is List < string > )
78
101
{
79
- foreach ( string jobID in JobID )
102
+ foreach ( string idOfJobToWait in ( JobID as List < string > ) )
80
103
{
81
- specifiedJobs . Add ( jobID ) ;
104
+ WriteDebug ( "Type of JobID filter is List<string>." ) ;
105
+ specifiedJobs . Add ( idOfJobToWait ) ;
82
106
}
83
107
}
108
+ else if ( ( JobID is PSObject ) && ( ( ( PSObject ) JobID ) . ImmediateBaseObject is string ) )
109
+ {
110
+ WriteDebug ( "Type of JobID filter is string." ) ;
111
+ specifiedJobs . Add ( ( JobID as PSObject ) . ImmediateBaseObject as string ) ;
112
+ }
113
+ else if ( JobID is string )
114
+ {
115
+ WriteDebug ( "Type of JobID filter is string." ) ;
116
+ specifiedJobs . Add ( JobID as string ) ;
117
+ }
118
+ }
119
+
120
+ WriteDebug ( "Number of jobs to wait on: " + specifiedJobs . Count ) ;
121
+
122
+ base . ExecuteCmdlet ( ) ;
123
+
124
+ ExecutionBlock ( ( ) =>
125
+ {
126
+ if ( ! TimeOut . HasValue )
127
+ {
128
+ TimeOut = new long ( ) ;
129
+ TimeOut = Int64 . MaxValue ;
130
+ }
84
131
132
+ List < string > pendingJobs = new List < string > ( specifiedJobs ) ;
85
133
DateTime waitingStartTime = DateTime . UtcNow ;
86
134
87
135
while ( true )
@@ -94,11 +142,16 @@ public override void ExecuteCmdlet()
94
142
95
143
bool areJobsRunning = false ;
96
144
97
- foreach ( string jobId in specifiedJobs )
145
+ for ( int i = 0 ; i < pendingJobs . Count ; i ++ )
98
146
{
99
- Mgmt . Job retrievedJob = AzureBackupClient . Job . GetAsync ( jobId , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result . Job ;
147
+ Mgmt . Job retrievedJob = AzureBackupClient . Job . GetAsync ( pendingJobs [ i ] , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result . Job ;
100
148
if ( AzureBackupJobHelper . IsJobRunning ( retrievedJob . Status ) )
101
149
areJobsRunning = true ;
150
+ else
151
+ {
152
+ pendingJobs . RemoveAt ( i ) ;
153
+ i -- ;
154
+ }
102
155
}
103
156
104
157
if ( ! areJobsRunning )
0 commit comments