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.

1
  • 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.? Commented Aug 8, 2022 at 19:38

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

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
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]"
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.
this one worked as expected, and i trying to use the switch statement to print only the Selected service part as karhtik said.
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}
|
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).

4 Comments

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.
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, to maintain the order of the entries / properties as defined, use [ordered] @{ ... } instead of @{ ... } - I've updated the answer to show this.
adding the [ordered] worked , thank you.

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.