如何在VSTS发布定义的PowerShell任务中传入参数替代JSON配置项
Got it, let's walk through this step by step to get your script pulling those four config values from VSTS release arguments instead of the JSON file:
Step 1: Revise the PowerShell Script (webjobs Stop.ps1)
Update the script to accept the four required parameters directly, and only keep the JSON read for the webJobs list. I've also fixed a few minor issues in the original script:
param( [string]$currentEnv, [string]$userName, [string]$password, [string]$webAppName, [string]$resourceGroup ) # Only fetch the webJobs array from the JSON file now [object]$paramObj = Get-Content "d:\a\r1\a\Jobs\WebJobs_scripts\WebJob_list.json" | ConvertFrom-Json [object[]]$webJobs = $paramObj.$currentEnv.webJobs # Optional: Add Azure authentication if your script doesn't handle it already # $securePassword = ConvertTo-SecureString $password -AsPlainText -Force # $credential = New-Object System.Management.Automation.PSCredential ($userName, $securePassword) # Connect-AzAccount -Credential $credential -ServicePrincipal -TenantId "your-tenant-id" foreach($wj in $webjobs) { if($wj.typeName -eq "continuous") { Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$webAppName/$($wj.name)" -Action stop -ApiVersion 2015-08-01 -Force Write-Host "Successfully stopped continuous web job: $($wj.name)" } }
Notes:
- I updated the
Write-Hostoutput to be more descriptive for debugging - Added optional Azure authentication code (uncomment and adjust if your script wasn't handling login)
- If you're using newer Azure modules, swap
Invoke-AzureRmResourceActionwithInvoke-AzResourceAction
Step 2: Clean Up the webjob_list.json File
Remove the four config fields we're now passing as parameters — only keep the webJobs array:
{ "Dev": { "webJobs": [ { "name": "webjobname", "typeName": "continuous" }, { "name": "webjobname", "typeName": "continuous" }, { "name": "webjobname", "typeName": "continuous" }, { "name": "webjobname", "typeName": "continuous" } ] } }
Step 3: Configure VSTS Release Definition
In your release definition's PowerShell task, navigate to the Arguments field and pass the parameters like this:
-currentEnv "Dev" -userName "abc-dev\$abcjobs-dev" -password "$(WebJobPassword)" -webAppName "abcjobs-dev" -resourceGroup "abc-Dev"
Critical Best Practice for Secrets
Never hardcode passwords in arguments! Instead:
- Go to your Azure DevOps project > Library > Variable Groups
- Create a secret variable (e.g.,
WebJobPassword) and paste your password - Link the variable group to your release definition
- Use
$(WebJobPassword)in the arguments — Azure DevOps will mask this value in logs automatically.
Step 4: Test the Release
Run your release to confirm the script receives all parameters correctly and stops the continuous web jobs as expected. You can add temporary Write-Host statements (avoid logging the password!) to debug parameter values if needed.
内容的提问来源于stack exchange,提问作者PDBR

