我在寫一些PowerShell cmdlet來自動配置Azure訂閱。其中一個用例是讓開發人員配置他們自己的環境。目前這需要大約20個步驟,並且容易出錯。將它們交給一些默認的天青cmdlet最終會比使用Microsoft的Azure GUI和一組指令帶來更多的錯誤。我想要一個腳本來完成配置過程,並抽象出很多簿記和錯誤檢查。有沒有辦法將Azure Powershell 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文件看,並提示您從中選出如果。您的輸入無效,它會再次提示。)
我想要的是像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.
}
}
}
上帝的母親。我的google-fu讓我失望了。謝謝。 – Zaaier