I need to install Az.Storage module in Azure windows VM through terraform VM extension - custom script. every other commands execute, but Module installation command does not execute. resource "azurerm_virtual_machine_extension" "VMextn202" {
name = "avmextn1"
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"fileUris": ["https://${azurerm_storage_account.Storage.name}.blob.core.windows.net/${azurerm_storage_container.Scripts.name}/${azurerm_storage_blob.BlobVMExt.name}"],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -file VMExt.ps1"
}
SETTINGS
}
Content of VMExt.ps1:
Get-Service | Out-File "C:\Users\Simulator-User\service.txt" -Force
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name Az.Storage -Force -AllowClobber -SkipPublisherCheck
Install-Module -Name Az.Storage -Force -AllowClobber -SkipPublisherCheck
Here, 1st cmd executes(service.txt is getting created in VM) 2nd cmd executes(package Provider is getting installed in VM) But next cmds related to module installations are not getting executed(cannot find modules from Get-InstalledModules - modules not getting installed in VM).
same thing is happening with manual creation of VM extension - custom script in Azure portal.
Any suggestions will help me.
In my case the problem was that the module is installed in a scope such that I could not see it when RDP to the VM.
So now I set the scope explicitly
then the module is available when I RDP and then Get-InstalledModules