Otomasyon PowerShell runbook Azure Resource Manager da bir uygulama şablonu dağıtma
Azure kaynağı dağıtan bir Otomasyon PowerShell runbook'unu, bir Azure Resource Manager oluşturabilirsiniz. Şablonlar, Azure kaynaklarınızı Azure Otomasyonu için şablon kullanmanızı sağlar. Şablonlarınızı Azure Resource Manager gibi merkezi ve güvenli bir konumda Depolama.
Bu makalede, yeni bir Azure Depolama hesabı dağıtmak için Azure Depolama'da depolanan bir Resource Manager şablonu kullanan bir PowerShell runbook'Depolama oluşturacağız.
Azure aboneliğiniz yoksa başlamadan önce ücretsiz bir hesap oluşturun.
Önkoşullar
Kullanıcı Azure Otomasyonu yönetilen kimliği olan bir hesap. Daha fazla bilgi için bkz. Bir hesap için kullanıcı tarafından atanan yönetilen Azure Otomasyonu kullanma.
Az modülleri:
Az.Accounts, , veAz.ManagedServiceIdentityAz.ResourcesAz.Storage. Otomasyon hesabına aktarıldı. Daha fazla bilgi için bkz. Az modüllerini içeri aktarma.Azure Depolama şablonun depolan Resource Manager hesabı.
Azure PowerShell makineye yüklenmiştir. Daha fazla bilgi Azure PowerShell için bkz. Azure PowerShell Modülünü Azure PowerShell. Az.ManagedServiceIdentity modülü de gerekir.
Az.ManagedServiceIdentitybir önizleme modülüdür ve Az modülünün bir parçası olarak yüklenmez. Yüklemek içinInstall-Module -Name Az.ManagedServiceIdentity
Yönetilen kimliklere izin atama
Runbook'ta depolamayla ilgili görevleri gerçekleştirmek için yönetilen kimliklere izin attayın.
Bağlan-AzAccount cmdlet'ini kullanarak Azure'da etkileşimli olarak oturum açın ve yönergeleri izleyin.
# Sign in to your Azure subscription $sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not($sub)) { Connect-AzAccount } # If you have multiple subscriptions, set the one to use # Select-AzSubscription -SubscriptionId <SUBSCRIPTIONID>Aşağıdaki değişkenler için uygun bir değer girin ve ardından betiği yürütün.
$resourceGroup = "resourceGroup" $automationAccount = "automationAccount" $storageAccount = "storageAccount" $userAssignedManagedIdentity = "userAssignedManagedIdentity" $storageTemplate = "path\storageTemplate.json" $runbookScript = "path\runbookScript.ps1"readercmdlet'ini yürütmek için rolü sistem tarafından atanan yönetilen kimliğeGet-AzUserAssignedIdentityattayın.$SAMI = (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccount).Identity.PrincipalId New-AzRoleAssignment ` -ObjectId $SAMI ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Reader"Depolama hesabına
Storage Account Contributoryönelik eylemler için rolü kullanıcı tarafından atanan yönetilen kimliğe attayın.$UAMI_ID = (Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup -Name $userAssignedManagedIdentity).PrincipalId New-AzRoleAssignment ` -ObjectId $UAMI_ID ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Storage Account Contributor"
Resource Manager şablonu oluşturma
Bu örnekte, yeni bir Azure Resource Manager hesabı dağıtan bir Depolama kullanırsınız. adlı bir yerel dosya storageTemplate.json oluşturun ve aşağıdaki kodu yapıştırın:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS",
"Premium_LRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-02-01",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "Storage",
"properties": {
}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[variables('storageAccountName')]"
}
}
}
Resource Manager şablonunu Azure Dosyalar
PowerShell kullanarak Bir Azure dosya paylaşımı oluşturun ve storageTemplate.json yükleyin. Dosya paylaşımı oluşturma ve dosya yükleme yönergeleri için Azure portal'de Kullanmaya başlayın'Azure Dosyalar ile Windows.
Aşağıdaki komutları çalıştırarak bir dosya paylaşımı oluşturun ve Resource Manager bu dosya paylaşımına yükleyin.
# Get the access key for your storage account
$key = Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccount
# Create an Azure Storage context using the first access key
$context = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $key[0].value
# Create a file share named 'resource-templates' in your Azure Storage account
$fileShare = New-AzStorageShare -Name 'resource-templates' -Context $context
# Add the storageTemplate.json file to the new file share
Set-AzStorageFileContent -ShareName $fileShare.Name -Context $context -Source $storageTemplate
PowerShell runbook betiği oluşturma
Azure Depolama'dan dosyayı alan ve şablonu dağıtarak yeni bir Azure Depolama storageTemplate.json oluşturun. adlı bir yerel dosya runbookScript.ps1 oluşturun ve aşağıdaki kodu yapıştırın:
param (
[Parameter(Mandatory=$true)]
[string]
$resourceGroup,
[Parameter(Mandatory=$true)]
[string]
$storageAccount,
[Parameter(Mandatory=$true)]
[string]
$storageAccountKey,
[Parameter(Mandatory=$true)]
[string]
$storageFileName,
[Parameter(Mandatory=$true)]
[string]
$userAssignedManagedIdentity
)
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with user-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
$identity = Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup `
-Name $userAssignedManagedIdentity `
-DefaultProfile $AzureContext
$AzureContext = (Connect-AzAccount -Identity -AccountId $identity.ClientId).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
-DefaultProfile $AzureContext
#Set the parameter values for the Resource Manager template
$Parameters = @{
"storageAccountType"="Standard_LRS"
}
# Create a new context
$Context = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageAccountKey
Get-AzStorageFileContent `
-ShareName 'resource-templates' `
-Context $Context `
-path 'storageTemplate.json' `
-Destination 'C:\Temp' -Force
$TemplateFile = Join-Path -Path 'C:\Temp' -ChildPath $storageFileName
# Deploy the storage account
New-AzResourceGroupDeployment `
-ResourceGroupName $resourceGroup `
-TemplateFile $TemplateFile `
-TemplateParameterObject $Parameters
Runbook'un içeri aktarın ve Azure Otomasyonu yayımlayın
Runbook'ı Otomasyon hesabınıza içeri aktarın ve ardından runbook'unu yayımlayın. Runbook'ları içeri aktarma ve yayımlama hakkında Azure portal için bkz. runbook'ları Azure Otomasyonu.
Otomasyon hesabınıza runbookScript.ps1 Bir PowerShell runbook'unu olarak içeri aktarmak için aşağıdaki PowerShell komutlarını çalıştırın:
$importParams = @{
Path = $runbookScript
ResourceGroupName = $resourceGroup
AutomationAccountName = $automationAccount
Type = "PowerShell"
}
Import-AzAutomationRunbook @importParams
# Publish the runbook
$publishParams = @{
ResourceGroupName = $resourceGroup
AutomationAccountName = $automationAccount
Name = "runbookScript"
}
Publish-AzAutomationRunbook @publishParams
Runbook’u başlatma
Şimdi Start-AzAutomationRunbook cmdlet'ini çağırarak runbook'a başlayacağız. Bir runbook'unçalışma Azure portal için bkz. Azure Otomasyonu. .
PowerShell konsolunda aşağıdaki komutları çalıştırın:
# Set up the parameters for the runbook
$runbookParams = @{
resourceGroup = $resourceGroup
storageAccount = $storageAccount
storageAccountKey = $key[0].Value # We got this key earlier
storageFileName = "storageTemplate.json"
userAssignedManagedIdentity = $userAssignedManagedIdentity
}
# Set up parameters for the Start-AzAutomationRunbook cmdlet
$startParams = @{
resourceGroup = $resourceGroup
AutomationAccountName = $automationAccount
Name = "runbookScript"
Parameters = $runbookParams
}
# Start the runbook
$job = Start-AzAutomationRunbook @startParams
Runbook çalıştırktan sonra, iş nesnesinin özellik değerini alarak durumunu kontrol $job.Status edin.
Runbook, Resource Manager şablonunu alır ve yeni bir Azure Depolama dağıtmak için kullanır. Aşağıdaki komutu çalıştırarak yeni depolama hesabının oluşturulmuş olduğunu görüyorsunuz:
Get-AzStorageAccount
Sonraki adımlar
- Şablon oluşturma hakkında daha fazla Resource Manager için bkz. Azure Resource Manager genel bakış.
- Azure Depolama'i Depolama..
- Runbook'ların diğer Azure Otomasyonu bulmak için bkz. Runbook'ları ve modülleri Azure Otomasyonu.