Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Defining a object array in a C# custom type

$
0
0

I'm pulling information from a web service using New-WebServiceProxy with GetRecords, and manipulating how it displays in PS using a custom type.  For the sake of the question, the PS code looks like:

function Get-ServerInfo {
    param (
        [Parameter(ValueFromPipeline=$true)]$ServerName
        )
    begin {
        Add-Type @"
            namespace MyTypes {
                public class ServerItem {
                    public string server_name ;
                    public string ip_address ;
                    public bool ip_active ;
                    }
                }
"@

        $server = New-Object MyTypes.ServerItem
        $uri = 'https://mysite.com/servers_list.do?WSDL'
        $conn = New-WebServiceProxy -Uri $uri
        $getRecords = new-object ( $conn.GetType().Namespace + '.getRecords')
        }
    process {
        $getRecords.server_name = $ServerName
        $SNConn.getRecords($getRecords) | %{
            New-Object MyTypes.ServerItem -Property @{
                name       = $_.name
                ip_address = $_.ip_address
                ip_active  = $_.ip_active
                }
            }
        }
    }
Get-ServerInfo Server1 | ft-autosize

name   ip_address     ip_active
----   ----------     ---------
Server1 148.89.245.124      True
Server1 127.0.0.1          False

The WSDL that I'm using is doing an outer join between the server object table, and the IP table, so if the server has two assigned IPs, it returns two records.  I would like to add a property to the object like IPInfo, that would retain the ip_address and ip_active structure, but allow be to store it as a single instance on the object I'm returning.  So that the return for the server would look like:

Get-ServerInfo Server1

name    ip_info
----    ----------
Server1 {148.89.245.124,127.0.0.1}

( Get-ServerInfo Server1 ).ip_info

ip_address     ip_active
----------     ---------
148.89.245.124      True
127.0.0.1          False

Do I need to define a public Object[] ip_info, or something else.  Examples would be appreciated, as this is my first foray into the C# side.


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>