我有一個關於Login-AzureRmAccount cmdlet的問題,我試圖圍繞它來包裹我的頭。當我運行Login-AzureRmAccount並使用我的憑據進行身份驗證時,憑證對象是否存儲在任何位置?如果我正在編寫一個包含大量Azure cmdlet的腳本,那麼是否需要保留這些憑據或以某種方式將這些憑證存儲到其他cmdlet中,或者獲得某種類型的身份驗證令牌,該令牌會與我保持一段時間?我對此做了一些研究,如果我的google-fu在這個主題上很弱,我很抱歉。謝謝!是Login-AzureRmAccount一個對象,還是我需要將它存儲在一個?
回答
實際上,現在您可以使用上下文持久性了,這樣做的用處不大。如果運行Enable-AzureRmContextAutosave
,則PowerShell會將此上下文容器存儲在'%AppData%\ Roaming \ Windows Azure PowerShell'中,以便下次打開PowerShell窗口時不需要再次登錄。
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'"
登錄-AzureRmAccount返回AzureContextContainer
對象存儲容器上下文。你可以可以存儲這個並且以後用Set-AzureRmContext
或者Save-AzureRmContext
。主要可以通過腳本捕獲和使用該對象,並向shell窗口顯示希望的相關信息。
需要注意的是,目前新的上下文爲每個PowerShell窗口創建,還有的cmdlet,讓您保存(Save-AzureRmContext
)和進口(Import-AzureRmContext
)上下文,這樣你可以給你的用戶配置文件中添加了這樣的命令和總是從相同的上下文開始。 。由於新功能將允許一組上下文在PowerShell會話中自動保存,因此這將在夏季進行更改。
- 1. 我的JSON是一個數組還是一個對象?
- 2. 我需要一個循環還是一個嵌套循環?
- 3. 我需要一個類變量還是一個實例變量?
- 4. 我需要一個View還是一個ViewController?
- 5. 我有一個JSON對象,它是在dymanic長度,需要處理它
- 6. JavaScript語法:什麼是({})它是一個函數還是對象?
- 7. pthread_cond_t是一個對象還是什麼?
- 8. 我需要一個浮動或對齊還是什麼?
- 9. 打印的對象是一個數組,它是一個對象
- 10. 我應該傳遞一個對象還是一個原語?
- 11. 表單應該包含一個對象還是一個對象?
- 12. 我需要知道一個矩陣是旋轉還是另一個鏡像
- 13. 可以識別這個還是我需要一個數組?
- 14. 我需要一個存儲過程嗎
- 15. 從對象,它是一個對象
- 16. 是一個IQueryable查詢還是隻能查詢一個對象?
- 17. 我應該每次創建一個新對象還是創建一個對象?
- 18. 在將另一個對象分配給它的引用之前,是否需要釋放我的實例對象?
- 19. 如果我只在一個方法的範圍內使用它,我是否需要保留一個對象?
- 20. copy.deepcopy還是新建一個對象?
- 21. 如何檢查JSON數據是一個對象還是一個對象數組?
- 22. 是否存在,因爲它是用一個字符串對象
- 23. do-while循環需要一個int還是一個字符串?
- 24. 我應該將PDO對象傳遞給每個對象,還是爲每個對象創建一個新對象?
- 25. 嵌套的對象是一個字符串 - 需要它們作爲對象
- 26. 如果我使用render_to_response,是否需要有一個HttpRequest對象?
- 27. 我是否需要繼續創建一個圖形對象
- 28. 我是否需要釋放一個返回的NSError對象?
- 29. 我想要搜索存儲在一個JSON對象,在JavaScript
- 30. 使用linqTOsql的MVC 2站點是否需要每個表對象的存儲庫,還是每個站點應該有一個存儲庫?
這是真的,您可以將容器對象導出到文件。如果您稍後要導入上下文/憑據,以避免您的腳本提示您輸入您的活動憑據,這很有幫助... PS C:\> Save-AzureRmContext -Profile(Add-AzureRmAccount)-Path C:\ test .json then PS C:\> Import-AzureRmContext -Path C:\ test.json – WebDrive