How to pass multiple webjobs as a parameter to the

2019-07-25 14:11发布

问题:

The below script is provided to start the Webjobs in the azure.But I am including the Json file into the Powershell script and running the Webjobs.Its good for only one environment.But I want the same script to be used for multiple environments with different Webjob names.So how can I modify this script so that I can stop and start the Webjobs in VSTS at release definition,Currently,I have 4 Webjobs for Dev Environment and I have 8 Webjobs for Int Environment with different azure subscriptions.So how can I use the below script for different environments.Kindly help me on this.I can't use the below script for all the environments because I am importing the only one json file.So it will only accept the particular json parameters.But in my case the json parameters would be different for different environments.So how can I import different json file contents into the below script? Is there is any possibility to include multiple json files and script can only accept particular Json script based on the environment which I run at the release definition in VSTS and can run accordingly.

Parameter.json:

{
    "userName": "user1",
    "password": "password1",
    "webAppName": "webapp1",
    "resourceGroup": "resourceGroup1",
    "webJobs": [   
      {
        "name": "abc",
        "typeName": "continuous"
      },
      {
        "name": "def",
        "typeName": "continuou‌s"
      }
    ]
  }

Script:

[object]$paramObj=Get-Content "PowerShellModuleProject1\parameter2.json"|ConvertFrom-Json 
        $userName =$paramObj.userName
        $password =$paramObj.password
        $webAppName =$paramObj.webAppName
        $resourceGroup=$paramObj.resourceGroup
        [object[]]$webJobs=$paramObj.webJobs
        foreach($wj in $webjobs){
         if($wj.typeName -eq "continuous")
         {
    Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$webAppName/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force
          Write-Host "continuous"
         Write-Host $wj.name
         }
         else{
         Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$webAppName/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force
         Write-Host "triggered"
         Write-Host $wj.name
         }
         }

回答1:

You can update Parameter.json file (parameter2.json) like this:

{
  "Dev": {
    "userName": "user1",
    "password": "password1",
    "webAppName": "webapp1",
    "resourceGroup": "resourceGroup1",
    "webJobs": [
      {
        "name": "abcDev",
        "typeName": "continuous"
      },
      {
        "name": "defDev",
        "typeName": "continuous"
      }
    ]
  },
  "Int": {
    "userName": "userInt",
    "password": "passwordInt",
    "webAppName": "webappInt",
    "resourceGroup": "resourceGroupInt",
    "webJobs": [
      {
        "name": "abcInt",
        "typeName": "continuous"
      },
      {
        "name": "defInt",
        "typeName": "continuous"
      }
    ]
  }
}

Script:

param(
[string]$currentEnv
)
[object]$paramObj=Get-Content "PowerShellModuleProject1\parameter2.json"|ConvertFrom-Json 
$userName =$paramObj.$currentEnv.userName
$password =$paramObj.$currentEnv.password
$webAppName =$paramObj.$currentEnv.webAppName
$resourceGroup=$paramObj.$currentEnv.resourceGroup
[object[]]$webJobs=$paramObj.$currentEnv.webJobs
foreach($wj in $webjobs){
         if($wj.typeName -eq "continuous")
         {
    Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$webAppName/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force
          Write-Host "continuous"
         Write-Host $wj.name
         }
         else{
         Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$webAppName/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force
         Write-Host "triggered"
         Write-Host $wj.name
         }
         }

Arguments: -currentEnv $(Release.EnvironmentName). Modify each environment’s name in release definition.

Or you can add environment variables with the same name (e.g. currentEnv) for each environment of release, and the Arguments: -currentEnv $(currentEnv)