Add-Xbox.ps1

function Add-Xbox
{
    <#
    .Synopsis
        Adds an xbox to xbox neighborhood
    .Description
        Adds an xbox to xbox neighborhood
    .Example
        Add-Xbox MyXboxOne
    #>

    [CmdletBinding(DefaultParameterSetName="Neighborhood")]
    param(
    # The name or IP of the xbox development kit
    [Parameter(ParameterSetName="Neighborhood",Position=0,Mandatory=$true,ValueFromPipeline=$true)]
    [Parameter(ParameterSetName="Group",Position=0,Mandatory=$true,ValueFromPipeline=$true)]
    [String[]]
    $Console,
    
    # The group or groups to add the console to
    [Parameter(ParameterSetName="Group", Position=1,Mandatory=$true)]
    [String[]]
    $Group,
    
    # If set, will connect to the xbox and output it on the pipeline
    [Switch]
    $PassThru    
    )
    
    begin {
        if (-not (Test-Path Variable:\XapXboxManager)) {
            $Script:XapXboxManager = New-Object Xdevkit.XboxManagerClass
        }    
    }
    process {
        switch ($psCmdlet.ParameterSetName) {
            Neighborhood {
                foreach ($c in $console) {
                    $script:XapXboxManager.AddConsole($c)
                    if ($PassThru) { Connect-Xbox $c } 
                }
            } 
            Group {
                $groups = @{}                               
                if ((Test-Path $psScriptRoot\XBPSConsoleGroup.clixml)) {
                    $existingGroups = Import-Clixml $psScriptRoot\XBPSConsoleGroup.clixml
                    $groups += $existingGroups
                }                                
                foreach ($g in $group) {
                    if (-not ($groups[$g])) { $groups[$g] = @() }
                    $groups[$g]+=$console
                }
                foreach ($key in @($groups.Keys)) {
                    $groups[$key] = @($groups[$key] | Select-Object -Unique)
                }
                $Groups | Export-Clixml $psScriptRoot\XBPSConsoleGroup.clixml
            }
        }
    }
}