You could use Azure Custom Script Extension to achiever your scenario.
The Custom Script Extension downloads and executes scripts on Azure
virtual machines. This extension is useful for post deployment
configuration, software installation, or any other configuration /
management task. Scripts can be downloaded from Azure storage or
GitHub, or provided to the Azure portal at extension run time.
If you want to use PowerShell to do it. You could write a PowerShell script and upload it to GitHub or Azure storage.
netsh advfirewall firewall add rule name="Open Port 5985" dir=in action=allow protocol=TCP localport=5985
The Set-AzureRmVMCustomScriptExtension
command can be used to add the Custom Script extension to an existing virtual machine. For more information, see Set-AzureRmVMCustomScriptExtension.
After your VM is created successful, you could execute the following cmdlet.
Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `
-VMName myVM `
-Location myLocation `
-FileUri myURL `
-Run 'myScript.ps1' `
-Name DemoScriptExtension
I suggest you could upload your script to GitHub, please ensure the script should be public. Just an example, it works for me.
Set-AzureRmVMCustomScriptExtension -ResourceGroupName shuiwinrm `
-VMName shui `
-Location "South Central US" `
-FileUri "https://gist.githubusercontent.com/Walter-Shui/432bc4e657b813522c6dea88ada2b0cd/raw/7fd9a04babf909f85a45a293a421bc703561ae15/winrm5985.ps1" `
-Run 'winrm5985.ps1' `
-Name DemoScriptExtension