2017-04-15 55 views
0

我有一個關於Login-AzureRmAccount cmdlet的問題,我試圖圍繞它來包裹我的頭。當我運行Login-AzureRmAccount並使用我的憑據進行身份驗證時,憑證對象是否存儲在任何位置?如果我正在編寫一個包含大量Azure cmdlet的腳本,那麼是否需要保留這些憑據或以某種方式將這些憑證存儲到其他cmdlet中,或者獲得某種類型的身份驗證令牌,該令牌會與我保持一段時間?我對此做了一些研究,如果我的google-fu在這個主題上很弱,我很抱歉。謝謝!是Login-AzureRmAccount一個對象,還是我需要將它存儲在一個?

回答

2

實際上,現在您可以使用上下文持久性了,這樣做的用處不大。如果運行Enable-AzureRmContextAutosave,則PowerShell會將此上下文容器存儲在'%AppData%\ Roaming \ Windows Azure PowerShell'中,以便下次打開PowerShell窗口時不需要再次登錄。

0

Login-AzureRmAccount沒有任何需要保留的對象。一旦你調用它,它會設置其他調用將使用的環境上下文。您可以修改此上下文,例如通過使用Set-AzureRmContext命令行開關來更改您命令將與之通信的訂閱和預訂。

我曾經需要使用的唯一的「上下文對象」是從New-AzureStorageContext返回的對象,它將使用環境上下文的Get-AzureStorageKey調用構建。該上下文對象將與來自Azure.Storage模塊的命令行一起使用,並將通過參數-context傳入。

這裏是一個腳本,我用它來創建一個新的存儲容器,同時顯示了abient上下文和使用

$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'" 
1

登錄-AzureRmAccount返回AzureContextContainer對象存儲容器上下文。你可以可以存儲這個並且以後用Set-AzureRmContext或者Save-AzureRmContext。主要可以通過腳本捕獲和使用該對象,並向shell窗口顯示希望的相關信息。

需要注意的是,目前新的上下文爲每個PowerShell窗口創建,還有的cmdlet,讓您保存(Save-AzureRmContext)和進口(Import-AzureRmContext)上下文,這樣你可以給你的用戶配置文件中添加了這樣的命令和總是從相同的上下文開始。 。由於新功能將允許一組上下文在PowerShell會話中自動保存,因此這將在夏季進行更改。

+0

這是真的,您可以將容器對象導出到文件。如果您稍後要導入上下文/憑據,以避免您的腳本提示您輸入您的活動憑據,這很有幫助... PS C:\> Save-AzureRmContext -Profile(Add-AzureRmAccount)-Path C:\ test .json then PS C:\> Import-AzureRmContext -Path C:\ test.json – WebDrive

相關問題