Microsoft Azure:如何在遍历多个订阅中的资源的脚本中更改订阅

时间:2018-09-19 01:01:05

标签: azure powershell

Adding the AzureRm.Profile version detail enter image description here我见过类似的Azure CLI线程,并且我正在使用Azure PowerShell控制台NOT CLI。我的理解是, Set-AzureRmContext 仅设置给定会话的脚本上下文。有没有办法在会话中间切换上下文?背景-我有一个脚本遍历它正在创建的每个资源,并且这些资源分布在多个订阅中。当它遍历行时,我需要它来更改订阅。我正在按名称进行操作(这将是理想的),因为每次运行Set-AzureRmContext都无法正常工作。简单设置和获取AzureRmContext测试告诉我它无法切换订阅上下文。所以不知道如何从这里继续。

我正在使用什么:

$resourceDetail = Import-csv $inputFile
$index = 1
$resourceDetail | foreach {
    Write-host "checking row $index"
    #subscription check and select
    $row = $_   
    $subscriptionName = $_.subscription
    $location = $_.location
    Write-Host "Subscription Name: $subscriptionName"
    Set-AzureRmContext -subscriptionId $subscriptionName
    Write-Host "subscription scope $subscriptionName"
    index = index +1
}

enter image description here Image that shows how the context refuses to change

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

如果订阅不在同一租户中,则可以使用PowerShell cmdlet Select-AzureRmSubscription -SubscriptionId subscriptionId更改当前订阅事件。但是重要的是,要更改的订阅必须位于同一帐户中。有关在此处更改订阅的示例:

enter image description here

答案 1 :(得分:0)

最新更新为 doc ,它已更改为

#include <iostream> class Field { public: int *data; Field() : data(new int[100]) {} ~Field() { delete[] data; std::cout << "Field is destroyed"; } }; class Base { int c; }; // Derived class, contains a non-trivial non-static member class Core : public Base { Field A; }; int main() { Base *base = new Core; delete base; // won't delete Field }

Set-AzureSubscription