Search code examples
windowsazurepowershellvirtual-machineuser-data

How to add a powershell script/command as userdata for Windows VMs in Azure


I am trying out the feature of userdata provided by Azure to pass scripts/metadata to VMs running on Azure.

For Linux VMs, I have tried passing a bash script, which involves creating a file. But I am trying the same for Windows VMs, but however the script doesn't get executed.

Through Azure's IMDS, I queried the userdata, and it returned the userdata that I passed when I created the VM.

So I am not really sure what's going wrong,whether I am passing the powershell commands in the wrong syntax etc.

This the command I passed as userdata to Azure Windows VM : New-Item -Path C:\Users\Public\tstfile2.txt -ItemType File , I am trying to create a temporary file in a directory and I am trying to verify whether the command passed as userdata get's executed or not but doing a Get-ChildItem -Path C:\Users\Public\ -Name and checking if the newly created file is present or not.

If someone has any idea/insights, please help!

Thanks.


Solution

  • I tried to reproduce the issue in my environment and got the below results I have created new file and executed the below command using PowerShell.

    New-Item -Path C:\Users\Public\tstfile3.txt -ItemType File
    

    When I execute the command I got the newly created file in below

    enter image description here

    I have used the below command to check the file is available or not

        Get-ChildItem -Path C:\Users\Public\ -Name
    

    enter image description here

    I have created the Windows VM

    enter image description here

    In the windows VM ,

    • Click on run command
    • Click on RunPowerShell script
    • Write the PowerShell script
    • Click on Run

    enter image description here

    I got the newly created file in the below

    enter image description here