我正在编写一些PowerShell cmdlet来自动配置Azure订阅 . 其中一个用例是让开发人员配置他们自己的环境 . 目前,这需要大约20个步骤并且容易出错 . 将它们交给一些默认的azure cmdlet会比使用Microsoft的Azure GUI和一组指令更容易出错 . 我想要一个脚本来完成配置过程,并抽象出大量的簿记和错误检查 .
我尝试在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文件查看的当前订阅,并提示您从中选择 . 如果您的输入无效,则会再次询问 . )
我想要的是像 Set-MyAzureSubscription
这样的自定义cmdlet,其中包含所有这些逻辑 . 后来我可以将它连接到 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.
}
}
}
1 回答
这个MSDN topic显示了如何从C#cmdlet实现中轻松调用另一个cmdlet .