'Set-WinSystemLocale and Set-Culture not working when called via Azure ARM Template Extensions Customer Script Extension

Background

I am setting up an ARM template to deploy Windows VMs and have written PowerShell scripts that I want to run after the VM is created. After looking at different options I chose to use the VM Extension Custom Script Extension to achieve this.

Note: Don't think this is related but if someone is curious, I am calling the scripts one after the other by adding a line of code to run the next script at the end of the current script. Every other script works perfectly.

Question

For the final script, I am trying to set the time zone, location, and regional format. I know that the script runs because I can see the time zone being set to what I want, but the location and regional format remains the default.

If I log in to the server and run the exact same script, everything works fine. I can run it from the first script, which will automatically go through all of the scripts, or just the last script. Either way the location and regional format will be set. The changes stay after restarting the machine as well.

Will really appreciate it if someone can help me out!

Script

#Requires -RunAsAdministrator

[CmdletBinding()]

param (
    # Timezone ID
    [String]$TimeZoneID = "AUS Eastern Standard Time",

    # Region
    [String]$Region = "en-AU"
)

Set-TimeZone -Id $TimeZoneID    # Set timezone
Set-WinSystemLocale $Region     # Set location
Set-Culture $Region             # Set regional format

ARM VM Extension part (don't think this is where the problem is though)

{
    "copy": {
        "name": "extension",
        "count": "[parameters('numberOfVM')]"
    },
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "name": "[concat(variables('vmName')[copyIndex('extension')], '/PSscripts')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', variables('vmName')[copyIndex('extension')])]"
    ],
    "tags": "[variables('resourceTags')]",
    "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.10",
        "autoUpgradeMinorVersion": true,
        "settings": {
            "timestamp": "[parameters('timeUTC')]"
        },
        "protectedSettings": {
            "commandToExecute": "[concat('powershell -File \"./', last(split(parameters('scripts')[0], '/')), '\"', ' -SomeArgument ', parameters('argument'))]",
            "fileUris": "[parameters('scripts')]"
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source