Skip to content

Commit e102e83

Browse files
author
Maddie Clayton
authored
Merge pull request Azure#6147 from maddieclayton/asjobname
Improving naming for Long Running Operations
2 parents 1da16cd + 9b41a8f commit e102e83

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
1919
using Microsoft.Azure.ServiceManagemenet.Common.Models;
2020
using Microsoft.WindowsAzure.Commands.Common;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2122
using System;
2223
using System.Collections.Concurrent;
2324
using System.Diagnostics;
@@ -688,22 +689,78 @@ protected override void ProcessRecord()
688689
}
689690
}
690691

692+
private string _implementationBackgroundJobDescription;
693+
691694
/// <summary>
692695
/// Job Name paroperty iof this cmdlet is run as a job
693696
/// </summary>
694697
public virtual string ImplementationBackgroundJobDescription
695698
{
696699
get
697700
{
698-
string name = "Long Running Azure Operation";
699-
string commandName = MyInvocation?.MyCommand?.Name;
700-
if (!string.IsNullOrWhiteSpace(commandName))
701+
if (_implementationBackgroundJobDescription != null)
701702
{
702-
name = string.Format("Long Running Operation for '{0}'", commandName);
703+
return _implementationBackgroundJobDescription;
703704
}
705+
else
706+
{
707+
string name = "Long Running Azure Operation";
708+
string commandName = MyInvocation?.MyCommand?.Name;
709+
string objectName = null;
710+
if (this.IsBound("Name"))
711+
{
712+
objectName = MyInvocation.BoundParameters["Name"].ToString();
713+
}
714+
else if (this.IsBound("InputObject") == true)
715+
{
716+
var type = MyInvocation.BoundParameters["InputObject"].GetType();
717+
var inputObject = Convert.ChangeType(MyInvocation.BoundParameters["InputObject"], type);
718+
if (type.GetProperty("Name") != null)
719+
{
720+
objectName = inputObject.GetType().GetProperty("Name").GetValue(inputObject).ToString();
721+
}
722+
else if (type.GetProperty("ResourceId") != null)
723+
{
724+
string[] tokens = inputObject.GetType().GetProperty("ResourceId").GetValue(inputObject).ToString().Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
725+
if (tokens.Length >= 8)
726+
{
727+
objectName = tokens[tokens.Length - 1];
728+
}
729+
}
730+
}
731+
else if (this.IsBound("ResourceId") == true)
732+
{
733+
string[] tokens = MyInvocation.BoundParameters["ResourceId"].ToString().Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
734+
if (tokens.Length >= 8)
735+
{
736+
objectName = tokens[tokens.Length - 1];
737+
}
738+
}
739+
740+
if (!string.IsNullOrWhiteSpace(commandName))
741+
{
742+
if (!string.IsNullOrWhiteSpace(objectName))
743+
{
744+
name = string.Format("Long Running Operation for '{0}' on resource '{1}'", commandName, objectName);
745+
}
746+
else
747+
{
748+
name = string.Format("Long Running Operation for '{0}'", commandName);
749+
}
750+
}
704751

705-
return name;
752+
return name;
753+
}
706754
}
755+
set
756+
{
757+
_implementationBackgroundJobDescription = value;
758+
}
759+
}
760+
761+
public void SetBackgroundJobDescription(string jobName)
762+
{
763+
ImplementationBackgroundJobDescription = jobName;
707764
}
708765

709766
protected virtual void Dispose(bool disposing)

0 commit comments

Comments
 (0)