I am looking for a way to execute scripts on a Virtual Machine using Rest API. But the GitHub reference is redirecting me to page not found error. Kindly help me out.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@dileep12231 Thanks for the question!
The source you are looking at is for this document in actual: Run scripts in your Windows VM
The REST API link leads you to this page: Virtual Machines Run Commands - Run Command
Hope this helps.
@BhargaviAnnadevara-MSFT
I already visited this link but there are no examples such as how we can execute a local powershell script using api. If you can provide some sample that would be helpful
@dileep12231 Sure, you can execute the Run Command API for the desired VM from the Try It feature on the page or from Postman as follows:
Request URI:
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/runCommand?api-version=2019-03-01
Body:
{
"commandId": "RunPowerShellScript",
"script": [
"param(",
" [string]$arg1,",
" [string]$arg2",
")",
"Write-Host This is a sample script with parameters $arg1 $arg2"
],
"parameters": [
{
"name": "arg1",
"value": "Hello"
},
{
"name": "arg2",
"value": "World"
}
]
}
This should yield 202 as the response if the request is well-formed and accepted.
Note: Make sure to include the Authorization header with your token in case you try it with Postman.
The other ways to run PowerShell scripts in your Windows VM by using Run Command are mentioned here.
Hope this helps.
@BhargaviAnnadevara-MSFT
This helps. Thanks a ton!
@dileep12231 Sure, glad I could help! :)