powershellconvertto-json

How to Pass User Input to a JSON File using powershell


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.


Solution

  • 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