Connect-Xbox.ps1

function Connect-Xbox
{
    <#
    .Synopsis
        Connect-Xbox
    .Description
        Connects to an xbox 360. By default the connection times out after 8 hours and conversations time out after 1 hour.
    .Example
        Connect-Xbox MyXboxDevKit
    #>

    param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [String[]]
    $Console,
    
    [Switch]
    $DoNotShareConnection,
    
    [Timespan]
    $ConnectionTimeout = (New-TimeSpan -Hours 8),
    
    [Timespan]
    $ConversationTimeout = (New-TimeSpan -Hours 1)
    
    )
    begin {
        if (-not (Test-Path Variable:\XapXboxManager)) {
            $Script:XapXboxManager = New-Object Xdevkit.XboxManagerClass
        }
        $existingGroups = @{}
        if ((Test-Path $psScriptRoot\XBPSConsoleGroup.clixml)) {
            $existingGroups = Import-Clixml $psScriptRoot\XBPSConsoleGroup.clixml
        }
        $groups = @{} + $existingGroups
    }

    process {
        $consoleList = @()
        $resolvedConsoleList = @()
        $consoleList += $console
        for ($i = 0; $i -lt $consoleList.Count; $i++) {
            $c = $consoleList[$i]
            if ($groups.Keys -icontains $c) {
                $consoleList+=@($groups[$c])
                continue
            } else {
                $resolvedConsoleList+=$c
            }
        }
        foreach ($c in $resolvedConsoleList) {
            $consoleObject = $script:XapXboxManager.OpenConsole($c)
            if (-not $DoNotShareConnection) {
                $ConsoleObject.Shared = $true
            }
            $ConsoleObject.ConnectTimeout = $ConnectionTimeout.TotalMilliseconds
            $consoleObject.ConversationTimeout = $ConversationTimeout.TotalMilliseconds
            $ConsoleObject
        }
    }
}