0

I have a script which invokes a session on remote computer and run a script.

$pw = convertto-securestring -AsPlainText -Force -String '$(PWD_vSphere_AdminUser)'
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist ".\Administrator",$pw
Invoke-Command -Credential $Cred -ComputerName 'testcomputer' -FilePath "./addvm.ps1" 

This works fine. Now I have to run it on multiple computers. I want to read -ComputerName values from a tfvars file (json format). where name = "testcomputer" occurring multiple times. I want to read this "name" value from tfvars file (json format) and run my script for each "name" value.

    {
  "vm": [
    {
      "testcomputer": [
        {
          "memory": "4096",
          "name": "testcomputer",
          "time_zone": "020"
        }
      ],
      "testcomputer1": [
        {
          "memory": "4096",
          "name": "testcomputer1",
          "time_zone": "020"
        }
      ],
      "testcomputer2": [
        {
          "memory": "4096",
          "name": "testcomputer2",
          "time_zone": "020"
        }
      ]
    }
  ]
}

I have read this Powershell retrieving a variable from a text file but that dosen`t solve much about occurrence.

8
  • Please add the code you're using to read the file and to then call Invoke-Command Commented Mar 28, 2022 at 21:08
  • @SantiagoSquarzon I dont have a code to read the file yet. I am new in powershell. I wrote code only for a single Invoke-Command. Commented Mar 28, 2022 at 21:11
  • The file you're showing us does literally look like the one you've shared? Do you have a way to modify the output of the process generating this file for a standard format like Json, Xml or CSV? Commented Mar 28, 2022 at 21:14
  • yes this is actual file, its a terraform variable file. test.tfvars Commented Mar 28, 2022 at 21:18
  • According to this answer stackoverflow.com/a/55059439/15339544 you can have terraform output as Json and I would recommend you to do so. The file you currently have would require unnecessary parsing. Commented Mar 28, 2022 at 21:46

1 Answer 1

0

If what you share is really the input you will get literally, one way to deal with it would be to use Regex, but if can somehow input that as an object, either JSON or XML or whatever that can be read and properly converted in to an object, you should do that instead of Regex.

As of regex, reading from literally the input you showed , you can do something like this :

$inputVar = @"
vm = {
    "testcomputer" = {
        name = "testcomputer"
        memory = "4096"
        time_zone = "020"
    }
    "testcomputer1" = {
        name = "testcomputer1"
        memory = "4096"
        time_zone = "020"
    }
    "testcomputer2" = {
        name = "testcomputer2"
        memory = "4096"
        time_zone = "020"
    }
}
"@


#the matching pattern.
$regexFormat = '(?<=name = ").[^"]*'

#Grab only the matching pattern items.
$computerList = [regex]::Matches($inputVar, $regexFormat) | ForEach-Object { $_.value }

foreach ($computer in $computerList) {

    $pw = convertto-securestring -AsPlainText -Force -String '$(PWD_vSphere_AdminUser)'
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist ".\Administrator", $pw
    Invoke-Command -Credential $Cred -ComputerName $computer -FilePath "./addvm.ps1"

}

I have made a literally input with the $inputVar just to show the example, and then you can do foreach loop to run your code for each computer.

Sign up to request clarification or add additional context in comments.

3 Comments

Terraform is not taking input syntax. So I have changed the tfvars as json format. Modified the question now.
also can I use a file as for $computerList. Like below $computerList = Get-Content -Path .\a.tfvars | [regex]::Matches($inputVar, $regexFormat) | ForEach-Object { $_.value }
That worked. Thanks very much. Just modified like below. $inputVar = Get-Content -Raw -Path a.tfvars #Grab only the matching pattern items. $computerList = [regex]::Matches($inputVar, $regexFormat) | ForEach-Object { $_.value }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.