1
votes

I am trying to run a Script using Custom Script extension on the VM. I am able to upload my Custom Script ( on my local drive) navigating via UI in the Extension settings but I am not able to do the same via CLI / Powershell

I am looking for az vm extension (CLI) / Set-AzVMCustomScriptExtension command that can help me to do the above i.e. to

Upload / Run a custom script extension for a script present @ local disk or Azure Repo

I have tried various ways the closest I could get is Set-AzVMCustomScriptExtension -FileUri "file:///D:/scripts/TINFAD01.ps1" -ResourceGroupName p1cad-draas-sb-dev-dev -Location eastus -VMname TINFAD01 -Verbose -Name TINFAD01DSC -Run 'TINFAD01.ps1' | Update-AzVM -Verbose

Gives an error of : Part of the path not found ( I have double checked the path and it does exist)

2
COuld you please tell me what is "upload a Custom script to Azure"? Is that you upload your script to Azure storage?Jim Xu
Please check my updated description, thank you :)Manan Shah

2 Answers

2
votes

According to my test, if you want to run a Script which is on the local file system using Custom Script extension, we need to upload the script to Azure storage. Then we can run the script. For more details, please refer to the document. For example: 1. Upload script to Azure storage.

  1. Run the script
Set-AzVMCustomScriptExtension -ResourceGroupName nrg-win -VMName n-win01 -Location eastus -FileUri "https://nlistorageaccntv1.blob.core.windows.net/scripts/myScript.ps1" -Run 'myScript.ps1' -Name DemoScriptExtension

enter image description here

Besides, if you just want to run the local script and do not want to upload it to Azure Storage, you can use Azure VM Run Command feature. For more details, please refer to the document

For example:

Connect-AzAccount
Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}
0
votes

NEW ANSWER based on edited/modified question:

Did you had a look here, it is possible to store your PS script in BlobStorage and reference it as the script location for the ExtensionScript CLI command:

https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#script-location and https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#extension-schema

And here you find an example on how to execute the CLI command: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#using-multiple-scripts


OLD OBSOLETE ANSWER

I guess you want to copy the script to a Windows VM.

One possible solution install a sftp or ssh server on the Windows VM and use sftp or scp.

Another solution would be to leverage Azure Files and attach the network drive to the VM and your local machine and then copy the script there centraly.

You could also leverage azcopy (https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-files) and first copy the scripts to a blob storage and on the VM in Azure you copy them back to local.

Maybe the best approach would be to leverage Azure DevOps (free tier available) and create a Pipeline in which you are using a file copy task to put your scripts on the target VM (https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-file-copy?view=azure-devops).