Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.

If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!

Hadle JSON to replace array usage



  • Hi folks,

    I need some help to use json value and any help would be appreciated.

    I need to assign a specific port for a specific web site.
    Actually, I am doing this:

    Role IIS:

    • @IISites=@(Site11,Site12,Site12,Site21,Site22,Site31,Site13)
    • @IISPorts=@(80,81,82,86,87,89,8080)

    Role Site1

    • @IISSitesSite1=@(Site11,Site12,Site13)

    Configuration for role Site1

     foreach $Site in @IISSitesSite1
     {
               set $Port = @IISPorts[$ListIndexof(@IISSites,$Site)];
               set $IISRoot = D:\$Site;
               Ensure-Directory $IISRoot;
            # Site IIS
            {
                IIS::Ensure-Site $Site
                (
                    Path: $IISRoot ,
                    Bindings: @(
        %(
            IPAddress: *, 
            Port: $Port, 
            HostName: *, 
            Protocol: http
               )
           )
        );
     }
    
     It working very  well but it is not pretty
          
         I am looking to handle the port assignement  **set $Port = @IISPorts[$ListIndexof(@IISSites,$Site)];** by using JSON but I miss sample and documentation so any suggestion would be appreciated
         
         Best Regards
    
    Product: Otter  
    Version: 2.0.8

  • inedo-engineer

    Is this what you're looking for?

    set $SitesJson = >>[
        {
            "Name": "Site11", 
            "Bindings": [{
                "IPAddress": "*",
                "Port": 80,
                "HostName": "*",
                "Protocol": "http"
            }]
        },
        {
            "Name": "Site12", 
            "Bindings": [{
                "IPAddress": "192.0.2.0",
                "Port": 81,
                "HostName": "site12.local",
                "Protocol": "http"
            }]
        }
    ]>>;
    
    foreach %Site in @FromJson($SitesJson)
    {
        set $SiteName = %Site.Name;
    
        IIS::Ensure-Site $SiteName
        (
            Path: C:\tmp\$SiteName,
            Bindings: %Site.Bindings
        );
    }
    

    Note the JSON string can come from anywhere (e.g. can be saved in an Asset and pulled then read with Get-Asset tmp.json and $FileContents(tmp.json)).

    Also if you want, you can skip the JSON altogether and just set a map from the start:

    set @Sites = @(
        %(
            Name: Site11,
            Bindings: @(
                %(
                    IPAddress: *,
                    Port: 80,
                    HostName: *,
                    Protocol: http
                )
            )
        ),
        %(
            Name: Site12,
            Bindings: @(
                %(
                    IPAddress: 192.0.2.0,
                    Port: 81,
                    HostName: site12.local,
                    Protocol: http
                )
            )
        )
    );
    

    Though it's probably easier to read the JSON, so that's just personal preference.



  • Thank you John

    This is exactly what I was looking for.

    You save my time 😁 and I really appreciate the time you have spent to answer me 😊

    Best regards


Log in to reply
 

Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation