2

I am creating a PowerShell script using Convert To-JSON cmd and i achieved that using below

    $body = @{
            devices = @(
                @{ 
                    credentials = @(
                        @{
                            label = 'username'
                            value = 'myname'
                            sensitive = 'false'
                        },
                        @{
                            label = 'Password'
                            value = 'Password@!'
                            sensitive = 'true'
                        }
                    )
                    services = @(
                       @{
                            name = 'RDP'
                            url = "https://$inputIpAddress/?pauth=[proxy_token]&data=[connection:$inputUsername]"
                            instructor = 'false'
                        },
                       @{
                            name = 'HTTPS'
                            url = "https://$inputIpAddress/?pauth=[proxy_token]&data=[connection:$inputUsername]"
                            instructor = 'false'
                        },
                       @{
                            name = 'SSH'
                            url = "https://$inputIpAddress/?pauth=[proxy_token]&data=[connection:$inputUsername]"
                            instructor = 'false'
                         }
                    connections = @(
                        @{
                            id = 'myname-rdp'
                            protocol = 'rdp'
                            hostname = "192.168.1.6"
                            port ='3389'
                        }
                        )
                       Parameters = @( 
                       @{
                            name = 'username'
                            value = 'myname'
                        },
                        @{
                            name = 'password'
                            value = 'Password@!'
                        }
                    )
                }
            )
        }

i am converting the above powershell to JSON File($body | ConvertTo-Json -Depth 4) and i would like to replace pass the arguments for the Username and IP Address and store it with the username.json every time while converting to JSON.

i have tried the Read-host to get the input from the user but i am facing difficulty to pass that input before printing the output.

karhtik
  • 506
  • 3
  • 15
  • i think you are trying to create a template script and they by using that creating multiple JSON file and pass the Username and IP address as a arguments.? – T.Anand Aug 08 '22 at 19:38

2 Answers2

2

For the IP address, you can set the new value by defining the parameter using standard dot-notation.

In the case of username, because Parameters is an array with duplicate object names, doing $body.devices.Parameters.name would return both the username and password objects, so you can filter the array to only select the object where the name is username

$inputUsername = Read-Host "Input Username"
$inputIpAddress = Read-Host "Input IP address"

( $body.devices.Parameters | Where-Object { $_.name -eq 'username' } ).value = $inputUsername
$body.devices.connections.hostname = $inputIpAddress

$body | ConvertTo-Json -Depth 4
scottwtang
  • 1,740
  • 2
  • 3
  • 13
  • can we also replace the IP Address under URL, the same IP address has to print on $body.devices.services.url = $inputIpAddress. i have updated the question – karhtik Aug 08 '22 at 20:47
  • 1
    Yes, you can do the same as I showed, when defining the hashtable you'll likely want to insert the variable there, like in mklement0's answer; `url = "$($inputIpAddress)/?pauth=[proxy_token]&data=[connection:username-rdp]"`, otherwise in the line where you set the new value, you'll repeat the entire string `$body.devices.services.url = "$($inputIpAddress)/?pauth=[proxy_token]&data=[connection:username-rdp]"` – scottwtang Aug 09 '22 at 14:34
  • 1
    i have used it like this and it is working `url = "https://$inputIpAddress/?pauth=[proxy_token]&data=[connection:$inputUsername]"` and i would like to print only the selected service either RDP or HTTPS or SSH. – karhtik Aug 12 '22 at 15:56
  • this one worked as expected, and i trying to use the switch statement to print only the Selected service part as karhtik said. – T.Anand Aug 12 '22 at 15:59
  • 1
    You can use the if and elseif statement to achieve only selected service, and pass the values to your services. `if ( $Services -eq 'rdp' ){Pass your values}` `elseif ( $Services -eq 'https' ) {Pass your values}` – T.Anand Aug 13 '22 at 17:19
  • @scottwtang, after the Conversion the values are not in a original format, i have tried to use the array format `ConvertTo-Json @($body) -Depth 100 | Format-Json`, but still the values are not as like the original. – karhtik Aug 13 '22 at 17:28
2

As an interpreted language, PowerShell allows you to construct literals based on values that are determined at runtime.

Therefore, simply place your hashtable literal (@{ ... }) after your Read-Host calls, and reference the variables in which you store the Read-Host responses in that literal.

A simplified example:

# Simulate two Read-Host calls (hard-code sample values).
$foo = 42
$bar = 'hi'

# Now define the hashtable literal and reference the variables assigned to above.
# Note: An [ordered] hashtable is used in order to retain the entries
#       in definition order.
[ordered] @{
  devices = @{
    foo = $foo
    bar = $bar
  }
}

Piping the above to ConvertTo-Json -Depth 4 yields:

{
  "devices": {
    "foo": 42,
    "bar": "hi"
  }
}

Note how the values of $foo and $bar were incorporated. You could even use the same technique in a loop, provided the hashtable literal is also in the loop body (after setting the variables it references).

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • but the values are not like the original format after the conversion, `ConvertTo-Json @($body) -Depth 100 | Format-Json` i have used the Format-json while converting to json but still no change. – karhtik Aug 13 '22 at 17:29
  • say for example 'credentials = @( )' is first array on the powershel but on the output it is printing on the last or second last and also the variables are also misplaced inside the array on the output. – karhtik Aug 15 '22 at 03:28
  • 1
    @karhtik, to maintain the order of the entries / properties as defined, use `[ordered] @{ ... }` instead of `@{ ... }` - I've updated the answer to show this. – mklement0 Aug 15 '22 at 13:28
  • 1
    adding the `[ordered]` worked , thank you. – karhtik Aug 17 '22 at 13:46