how to add quotes in the entire string by using powershell

roger chen 41 Reputation points
2022-03-02T22:24:23.153+00:00

I have this string

[{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]

i'm wondering how can I write a function to convert it to

[{"listenport":"443","connectaddress":"10.1.10.20","connectport":"443","firewallrulename":"port443","direction":"Inbound","action":"Allow","protocol":"TCP"},{"listenport":"80","connectaddress":"10.1.10.20","connectport":"80","firewallrulename":"port80","direction":"Inbound","action":"Allow","protocol":"TCP"}]

I have tried to use insert and indexof , but couldn't figure out how to do for an entire string

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,408 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,246 Reputation points
    2022-03-02T23:12:41.377+00:00
    $str = '[{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]'
    $new = $str.Replace(',','","').Replace('{','{"').Replace('}','"}').replace(':','":"')
    cls
    "Your original string is:"
    $str
    ""
    "Your new string is:"
    $new 
    

0 additional answers

Sort by: Most helpful