【问题标题】:How to handle multiple tenants in Azure subscriptions using PowerShell如何使用 PowerShell 处理 Azure 订阅中的多个租户
【发布时间】:2016-06-18 06:42:22
【问题描述】:
我在 Azure 中有三个订阅帐户,并且正在使用 java 为远程应用程序执行 PowerShell 命令。
我的 PowerShell 版本:
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
我有以下问题:
- 我需要能够从 java 开始在 PowerShell 中同时工作
不同的 Azure 帐户。
- Select-AzureSubscription 命令将帐户设置为默认帐户和当前帐户。但是,如果我在 Select-AzureSubscription 上使用 -Default,它会说 -Default 已弃用。
- 选择订阅后,我需要那里的所有命令
处理选定的订阅。
我应该如何维护它们以同时在不同的订阅上工作?
【问题讨论】:
标签:
java
windows
powershell
azure
azure-powershell
【解决方案1】:
由于未指定您安装的 Azure PowerShell 模块的版本,我建议您首先确保您拥有最新版本,可在 http://aka.ms/webpi-azps 获得。
未来管理这些多个环境的方法(新方法)是使用 Azure 配置文件。例如(使用虚构名称),
# Create a profile
Login-AzureRmAccount -SubscriptionId "[subscription id (GUID)]" -TenantId "adatum.onmicrosoft.com"
Save-AzureRmProfile -Path "C:\adatum" # Use a name meaningful to you.
# Create another profile
Login-AzureRmAccount -SubscriptionId "[subscription id (GUID)]" -TenantId "contoso.com"
Save-AzureRmProfile -Path "C:\contoso" # Use a name meaningful to you.
# Create another profile
Login-AzureRmAccount -SubscriptionId "[subscription id (GUID)]" -TenantId "fabrikam.com"
Save-AzureRmProfile -Path "C:\fabrikam" # Use a name meaningful to you.
然后,选择要用于会话的配置文件。例如,
Select-AzureRmProfile -Path "C:\contoso"
# Do work (invoke commands) on the Contoso tenant.
Select-AzureRmProfile -Path "C:\adatum"
# Do work (invoke commands) on the Adatum tenant.
等等……
【解决方案2】:
@哈里,
根据您的描述,我不确定您要使用 PowerShell 管理什么服务。为了更好地开发 PowerShell 脚本,我建议您可以安装PowerShell ISE。
如果你想使用Service management API来管理你的服务,你可以按照这个方法来设置你的订阅。
PS C:\Users\user> Add-AzureAccount
PS C:\Users\user> $subscriptionList=Get-AzureSubscription | fl SubscriptionName,SubscriptionId,IsDefault,IsCurrent
PS C:\Users\user> $subscriptionList
PS C:\Users\user> function DoAction($subscriptionID){ $subscriptionID}
PS C:\Users\user> function SubscriptionList($subscriptionList){ for($i=0;$i -lt $subscriptionList.Length;$i++){DoAction($subscriptionList[$i].SubscriptionId);$subscriptionList[$i].SubscriptionId}}
PS C:\Users\user> SubscriptionList -subscriptionList $subscriptionList
如果您想管理 Azure 资源组,我建议您使用 azure ARM 模块来处理您的操作,正如 @Rick 在上一篇文章中所说。你可以编写函数来执行你的逻辑。推荐你参考这个文档:http://azurefabric.com/powershell-1-0-and-arm-tenants-and-subscriptions/