Login-AzureRmAccount是一个对象还是我需要将其存储在一个对象中?

时间:2017-04-15 01:46:31

标签: powershell azure

我有一个关于Login-AzureRmAccount cmdlet的问题,我正试图绕过它。当我运行Login-AzureRmAccount并使用我的凭据进行身份验证时,凭据对象是否存储在任何位置?如果我正在编写一个包含大量Azure cmdlet的脚本,我是否需要保留这些凭据或以某种方式存储这些凭据以传递到其他cmdlet,或者我是否会获得某种与我保持一段时间的身份验证令牌?我对此做了一些研究,如果我的google-fu在这个问题上表现不佳,我会道歉。谢谢!

3 个答案:

答案 0 :(得分:2)

实际上,既然您可以使用上下文持久性,那么它就不那么有用了。如果您运行Enable-AzureRmContextAutosave,那么PowerShell会将此上下文容器存储在'%AppData%\ Roaming \ Windows Azure PowerShell'中,以便您下次打开PowerShell窗口时无需再次登录。

答案 1 :(得分:1)

Login-AzureRmAccount返回AzureContextContainer个对象。您可以存储此内容,稍后将其与Set-AzureRmContextSave-AzureRmContext一起使用。主要是脚本可以捕获和使用对象,并向shell窗口显示希望相关的信息。

请注意,目前为每个PowerShell窗口创建了一个新的上下文,有一些cmdlet允许您保存(Save-AzureRmContext)并导入(Import-AzureRmContext)一个上下文,以便您可以添加这样的对用户配置文件的命令,始终以相同的上下文开头。 。这将在夏季进行更改,因为新功能将允许一组上下文在PowerShell会话中自动保留。

答案 2 :(得分:0)

Login-AzureRmAccount没有任何需要保留的对象。一旦你调用它就会设置其他调用将使用的环境上下文。您可以修改此上下文,例如,使用Set-AzureRmContext命令行开关更改命令将与哪个订阅和内容进行通信。

唯一的"上下文对象"我曾经需要使用从New-AzureStorageContext返回的对象,该对象将使用环境上下文的Get-AzureStorageKey调用构建。该上下文对象将与Azure.Storage模块中的命令行开关一起使用,并将使用-context参数传入。

这是我用来创建新存储容器的脚本,该存储容器显示正在使用的上下文和存储容器上下文

$resourceGroupName = "MyResourceGroup"
$storageAccountName ="examplestorageaccount"
$storageContainerName = "zips"
$resourceGroupLocation = "centralus"


#******************************************************************************
# Script body
# Execution begins here
#******************************************************************************
$ErrorActionPreference = "Stop"

# sign in
Write-Host "Logging in...";
$null = Login-AzureRmAccount;

# select subscription
$subscriptionId = ( Get-AzureRmSubscription | Out-GridView -Title “Select an Azure Subscription …” -PassThru).SubscriptionId

Set-AzureRmContext -SubscriptionId $subscriptionId

# Register RPs
$resourceProviders = @("microsoft.storage");
if($resourceProviders.length) {
    Write-Host "Registering resource providers"
    foreach($resourceProvider in $resourceProviders) {
        $null = Register-AzureRmResourceProvider -ProviderNamespace $resourceProvider;
    }
}

#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
    Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
    New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
}
else{
    Write-Host "Using existing resource group '$resourceGroupName'";
}

#Create or check for existing storage account
$storageAccount = Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroup $resourceGroupName -ErrorAction SilentlyContinue
if(!$storageAccount)
{
    Write-Host "Account not found, creating new storage account $storageAccountName";
    $storageAccount = New-AzureRmStorageAccount -Location $resourceGroupLocation -Name $storageAccountName -ResourceGroupName $resourceGroupName -SkuName Standard_LRS -Kind Storage
}
else
{
    Write-Host "Using existing storage account $storageAccountName'";
}

$storageAccountKey0 = (Get-AzureRmStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
$storageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey0

#Create or check for existing storage container
$storageContainer = Get-AzureStorageContainer -Name $storageContainerName -Context $storageAccountContext -ErrorAction SilentlyContinue
if (!$storageContainer)
{
    Write-Host "Container not foind, creating new storage container $storageContainerName"
    $storageContainer = New-AzureStorageContainer -Name $storageContainerName -Permission Blob -Context $storageAccountContext
}
else
{
    Write-Host "Using existing storage container $storageContainerName'";
}

Write-Host "------------------------------------------------"
Write-Host "New defaults for WriteToStorageAccount.ps1"
Write-Host '$storageAccountName =' "'$storageAccountName'"
Write-Host '$storageContainerName =' "'$storageContainerName'"
Write-Host '$key =' "'$storageAccountKey0'"