-1

I have Json file having the below and Powershell script calling this Json file using the below script.But I want to call the Json values as arguments from VSTS Powershell task .Please help me out how to call inturn.

Given Below webjob_list.json I have user name, password, resource group, webapp name. I would like to call these 4 parameters as arguments via VSTS Release definition using Powershell task under argument section. I don't want to put these 4 values inside Json instead want to call as arguments from Powershell script.

Json Script:

WebJob_list.json:



    {
          "Dev": 
          {

            "userName": "abc-dev\\$abcjobs-dev",
            "password": "SKGGfiuqHdvJ09eCmhDmE4mBnL8PSAZhnQMxzb",
            "webAppName": "abcjobs-dev",
            "resourceGroup": "abc-Dev"
"webJobs": [   
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      }
    ]

      }

webjobs Stop.ps1:

param(
[string]$currentEnv
)

[object]$paramObj=Get-Content "d:\a\r1\a\Jobs\WebJobs_scripts\WebJob_list.json" |ConvertFrom-Json
$userName =$paramObj.$currentEnv
$password =$paramObj.$currentEnv
$webAppName =$paramObj.$currentEnv
$resourceGroup=$paramObj.$currentEnv
[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 stop -ApiVersion 2015-08-01 -Force
    Write-Host "continuous"
    Write-Host $wj.name
   }
}
PDBR
  • 331
  • 1
  • 7
  • 17
  • Not clear the question, do you need to read the data from json file? If not, what's the problem of specify them as parameters? – starian chen-MSFT Jun 04 '18 at 06:16
  • @starianchen-MSFT, I hope you remembered that you shared the above webjobs scripts to stop and start continuous webjobs few months ago.But my team is asking me to remove hard coded part in the json script like user name,password,webapp name,resource group and asked me to pass from variables section.I have put the 4 variables under variables section of release definition but how to change/modify my json and powershell scripts so that the link between the 2 scripts and how can the 2 scripts access the 4 variables from back ground under variables section ? Please help me on this. – PDBR Jun 04 '18 at 09:18
  • Since you don't need the json file, so you don't need to change json file. Just refer to Kenny's script to add parameters: `param(username, password, webappname, resourcegroup)` – starian chen-MSFT Jun 04 '18 at 09:30
  • @starianchen-MSFT,But In my Json I need to remove user name ,password,resource grp,webapp name right.So how can I modify Json.I don't want to show the values in the json ,those values I can pass through variables section.So how can I remove or how to modify my json? Should I put my json parameters like username: $username or can remove whole block in json ? – PDBR Jun 04 '18 at 09:53
  • Remove it directly and you don't need to use json file anymore, the $username value is the username. (specify in argument, e.g. `-username $(variablename)`), also remove related code in PowerShell script, `$userName =$paramObj.$currentEnv $password =$paramObj.$currentEnv $webAppName =$paramObj.$currentEnv $resourceGroup=$paramObj.$currentEnv` – starian chen-MSFT Jun 04 '18 at 10:00
  • @starianchen-MSFT,Thanks for the response quickly.I edited my json script which is original script.I need to use Json file to call webjob names but only the 4 variables I need to remove.So if possible can you please modify my script above as you suggested so that I will directly modify in my VSTS code.Please understand that I need to call webjobs using user name ,password,resourcegrpname and webjobsname.So kindly update accordingly. – PDBR Jun 04 '18 at 10:27
  • @starianchen-MSFT,Also please let me know how to declare those variables in the powershell script ? – PDBR Jun 04 '18 at 10:36
  • Check my answer. – starian chen-MSFT Jun 05 '18 at 05:05
  • @starianchen-MSFT, your solutions are always made me correct with a clear and detailed solution.Thanks a lot for your responses.Sorry I forgot to accept this answer. – PDBR Jun 20 '18 at 09:47
  • @starianchen-MSFT,Please help me to solve this issue as well as the explanation which andy is given is little bit complicated.Please help me out in a simple way. – PDBR Jun 20 '18 at 09:51

2 Answers2

0

Your script looks like it will already read the json, but to pass variables them between tasks you need to set them in VSTS from the script itself

Write-Host "##vso[task.setvariable variable=username]$username"

It's also possible to store your variables in VSTS itself from the outset which might be a good option?

VSTS > Releases > Pipeline > New

Next create a Dev environment

Click Variables link and create your 4 x variables.

Check that they are scoped to the Dev environment.

Then in your PowerShell task create a param block to accept the variables

param(username, password, webappname, resourcegroup)

Then in the Script Arguments box for the PowerShell task add the arguments as:

-username $(username) -password $(password) -webappname $(webappname) -resourcegroup $(resourcegroup)
committhisgit
  • 110
  • 1
  • 6
  • As I told earlier,I have Powershell script calling the test.json file using this line :[object]$paramObj=Get-Content "d:\a\r1\a\WebJobs_scripts\test.json" |ConvertFrom-Json. but when I try to run the Powershell task to call the abc.ps1 file I want to that Powershell script should call json and pass the json parameters in the Powershell argument section.I am using json script as well.Please see my above comments and do the needful. – PDBR Jun 01 '18 at 10:35
  • ,if you don't mind could you please edit my script with your suggestions so that I will understand as I am new to Powershell.Please understand and help me on this. – PDBR Jun 01 '18 at 13:11
0

Json file:

  {
      "Dev": {
      "webJobs": [   
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      },
      {
        "name": "webjobname",
        "typeName": "continuous"
      }
    ]

      }
}

Script:

param(
[string]$currentEnv,
[string]$username
[string]$password,
[string]$webAppName,
[string]$resourceGroup
)

[object]$paramObj=Get-Content "d:\a\r1\a\Jobs\WebJobs_scripts\WebJob_list.json" |ConvertFrom-Json
[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 stop -ApiVersion 2015-08-01 -Force
    Write-Host "continuous"
    Write-Host $wj.name
   }
}
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • ,Also can you able to respond to my query please @ https://stackoverflow.com/questions/50638075/how-to-pass-capabilities-as-a-environmental-variable-to-batch-script-while-runni/50678080?noredirect=1#comment88628500_50678080 – PDBR Jun 20 '18 at 09:48