如何将多个Web作业作为参数传递给PowerShell脚本 [英] How to pass multiple webjobs as a parameter to the PowerShell script

查看:84
本文介绍了如何将多个Web作业作为参数传递给PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供了以下脚本来启动Azure中的Webjobs,但是我将Json文件包含在Powershell脚本中并运行Webjobs,它仅对一个环境有用,但我希望同一脚本可用于多个环境因此,如何修改此脚本,以便可以在发行版定义时停止和启动VSTS中的Webjob,目前,我有4个用于Dev Environment的Webjob,而我有8个具有不同Azure订阅的Int Environment的Webjob.因此,如何在不同的环境下使用以下脚本,请对此提供帮助.由于仅导入一个json文件,因此无法在所有环境下使用以下脚本,因此它将仅接受特定的json参数.但在我的情况下,不同环境下的json参数会有所不同,那么如何将不同的json文件内容导入以下脚本中呢?是否有可能包含多个json文件,并且脚本只能接受基于我在VSTS中的发行版定义上运行的环境的特定Json脚本,并且可以相应地运行.

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:

Parameter.json:

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

脚本:

[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
         }
         }

推荐答案

您可以像这样更新Parameter.json文件(parameter2.json):

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"
      }
    ]
  }
}

脚本:

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
         }
         }

参数:-currentEnv $(Release.EnvironmentName).在发布定义中修改每个环境的名称.

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

或者您可以为每个发行版环境添加具有相同名称(例如currentEnv)的环境变量,以及参数:-currentEnv $(currentEnv)

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

这篇关于如何将多个Web作业作为参数传递给PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆