【发布时间】:2013-02-07 02:09:00
【问题描述】:
我正在编写一些 powershell cmdlet 来自动配置 Azure 订阅。其中一个用例是让开发人员配置他们自己的环境。目前这需要大约 20 个步骤并且容易出错。与使用 Microsoft 的 Azure GUI 和一组指令相比,交给他们一些默认的 azure cmdlet 会导致更多错误。我想要一个脚本,它可以在配置过程中牵着他们的手,并抽象出大量的簿记和错误检查。
我尝试在 Powershell 脚本中执行所有这些操作,但结果变得一团糟:
Function SelectSubscription()
{
$match = $False;
while(!($match))
{
Write-Host "Enter a subscription from the following list:";
DisplaySubscriptions;
$global:subscription = Read-Host " ";
(Get-AzureSubscription).GetEnumerator() | ForEach-Object
{
if ($_.SubscriptionName -eq $subscription)
{
Write-Host "Setting default subscription to: $subscription";
Set-AzureSubscription -DefaultSubscription $subscription;
$match = $True;
};
};
if (!($match))
{
Write-Host "That does not match an available subscription.`n";
};
};
}
(这会显示您可以在 .publishsettings 文件中看到的当前订阅,并提示您从中进行选择。如果您的输入无效,它会再次询问。)
我想要一个自定义 cmdlet,例如 Set-MyAzureSubscription,其中包含所有这些逻辑。稍后我可以将它连接到Get-Help。
所以我在 VS2010 中设置了 cmdlet,我想从自定义 cmdlet 中调用 Get-AzureSubscription。我可以通过打开一个 powershell 脚本的实例来调用 cmdlet……然后以编程方式将文本粘贴进去……但这似乎不太理想。
在此处详细了解该方法:Call azure powershell cmdlet from c# application fails
还有其他方法吗?这就是我目前在 C# 中所拥有的。
namespace Automated_Deployment_Cmdlets
{
[Cmdlet(VerbsCommon.Set, "CustomSubscription", SupportsShouldProcess=true)]
class CustomSubscription : PSCmdlet
{
[Parameter(Mandatory=true, ValueFromPipelineByPropertyName=true)]
public string DefaultSubscription { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
// Call Get-AzureSubscription, then do some stuff -- as above.
}
}
}
【问题讨论】:
标签: c# powershell azure powershell-cmdlet