查询:我想在不同的订阅中从控制台应用程序创建资源组并行。
我开发了小型控制台应用程序。我已经安装了Azure Powershell 3.0版。从控制台应用程序调用system.automation.dll的powershell。请找到我用过的代码
C#控制台应用程序:
class Program
{
static void Main(string[] args)
{
Program p=new Program();
var resourceGroups=new List<ResourceGroup>();
resourceGroups.Add(new ResourceGroup { AdUsername = "AdUsername", AdPassword = "AdPassword", SubscriptionId = "SubscriptionId1"});
resourceGroups.Add(new ResourceGroup { AdUsername = "AdUsername", AdPassword = "AdPassword", SubscriptionId = "SubscriptionId2" });
resourceGroups.Add(new ResourceGroup { AdUsername = "AdUsername", AdPassword = "AdPassword", SubscriptionId = "SubscriptionId2" });
resourceGroups.Add(new ResourceGroup { AdUsername = "AdUsername", AdPassword = "AdPassword", SubscriptionId = "SubscriptionId3" });
resourceGroups.Add(new ResourceGroup { AdUsername = "AdUsername", AdPassword = "AdPassword", SubscriptionId = "SubscriptionId1" });
Parallel.ForEach(resourceGroups, (resourceGroup,pls,index)=> ParallelAccessPowershellScript(resourceGroup, index));
}
private static void ParallelAccessPowershellScript(ResourceGroup resourceGroup,long i)
{
try
{
string scriptPath = @"Microsoft_Image\loadjsontemplate.ps1";
var addParametersToScriptFile = new Command(scriptPath);
addParametersToScriptFile.Parameters.Add(new CommandParameter("AdUsername", resourceGroup.AdUsername));
addParametersToScriptFile.Parameters.Add(new CommandParameter("AdPassword", resourceGroup.AdPassword));
addParametersToScriptFile.Parameters.Add(new CommandParameter("SubscriptionId",resourceGroup.SubscriptionId));
addParametersToScriptFile.Parameters.Add(new CommandParameter("ResourceGroupName", "testparallel"+i));
addParametersToScriptFile.Parameters.Add(new CommandParameter("LocationName", "location"));
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var powerShell = PowerShell.Create();
var scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned");
var pipeline = powerShell.Runspace.CreatePipeline();
pipeline.Commands.Add(addParametersToScriptFile);
pipeline.Invoke();
runspace.Close();
runspace.Dispose();
powerShell.Dispose();
}
catch (Exception e)
{
}
}
}
Powershell脚本
param
(
[Parameter(Mandatory = $true,HelpMessage="Azure Resource Group name,virtual network,availability are created in this name")]
[String]
$ResourceGroupName,
[Parameter(Mandatory = $true,HelpMessage="Azure Resource Group created in this location")]
[String]
$LocationName,
[string]
$AdPassword,
[string]
$AdUserName,
[string]
$SubscriptionId
)
#login into powershell using active directory credentials
$securePassword = ConvertTo-SecureString $AdPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($AdUserName, $securePassword)
Add-AzureRmAccount -Credential $credential
#select the subscription as user chosed
$AzureInfo = Get-AzureRmSubscription -SubscriptionId $SubscriptionId
Select-AzureRmSubscription -SubscriptionId $SubscriptionId -TenantId $AzureInfo.TenantId
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName
但是所有资源组都创建了相同的订阅。如何在不同的订阅中创建并行资源组?