Azure PowerShell: Set Custom Script Extension on VM

Here’s a script that sets/runs an existing custom script extension on a VM in Azure:

$ResourceGroupName = "MyResourceGroup"
$StorageAccountName = "mystorage"
$VmName = "MyWinVm01"
$ExtensionName = "TestScriptExtension"
$ExtensionContainerName = "extensions"
$ExtensionFileName = "test\TestScriptExtension.ps1"
$ResourceGroupLocation = (Get-AzureRmResourceGroup -Name $ResourceGroupName)[0].Location
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName)[0].Value
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroupName `
-Location $ResourceGroupLocation `
-VMName $VmName `
-Name $ExtensionName `
-StorageAccountName $StorageAccountName `
-StorageAccountKey $StorageAccountKey `
-ContainerName $ExtensionContainerName `
-FileName $ExtensionFileName 
#To remove the script extension from the VM, you would run...
#Remove-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroupName -VMName $VmName -Name $ExtensionName -Force

Hope this helps!

This entry was posted in Azure, PowerShell. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *