XboxLivePsModule.psm1

<#
    My Function
#>


function Set-XblSandbox(
    [parameter( Mandatory = $true )] [string] $SandboxId
)
{
    # Get the ID and security principal of the current user account
    $myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
    $myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);

    # Get the security principal for the administrator role
    $adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

    $setSandboxCommand = "Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\xboxlive -name Sandbox -Value " + $SandboxId + ";"
    $setSandboxCommand += "Restart-Service XblAuthManager;"

    # Check to see if we are currently running as an administrator
    if ($myWindowsPrincipal.IsInRole($adminRole))
    {
        # We are running as an administrator, so change the title and background colour to indicate this
        invoke-expression $setSandboxCommand
    }
    else 
    {
        # We are not running as an administrator, so relaunch as administrator

        # Create a new process object that starts PowerShell
        $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

        $newProcess.Arguments = "-command `"& {" + $setSandboxCommand +"}`""

        # Indicate that the process should be elevated
        $newProcess.Verb = "runas";

        # Start the new process
        $process = [System.Diagnostics.Process]::Start($newProcess);
        $process.WaitForExit()

        return Get-XblSandbox

    }
  
}

function Set-XblSandbox2(
    [parameter( Mandatory = $true )] [string] $SandboxId
)
{
    #Check if SandboxSwitcher.exe exsits.
    if(Test-Path -Path .\SandboxSwitcher.exe)
    {
        $p = Start-Process .\SandboxSwitcher.exe -ArgumentList $SandboxId -wait -Verb RunAs
        $p.HasExited
        
        $newSandbox = Get-XblSandbox
        if ($newSandbox -eq $SandboxId)
        {
            return $newSandbox;
        }
        else
        {
            Write-Error -Message "Switching sandbox failed";
        }
    }
    else
    {
        Write-Error -Message "SandboxSwitcher.exe is not found";
    }
}

function Get-XblSandbox()
{
    Param(
        [ValidateNotNullOrEmpty()]
        [string]
        $XboxName
    )

    if ([string]::IsNullOrEmpty($XboxName))
    {
        $RegKey = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\xboxlive
        $Sandbox = $RegKey.Sandbox

        if ([string]::IsNullOrEmpty($Sandbox))
        {
            $Sandbox = "RETAIL"
        }
    }
    else #Remote console sandbox
    {
        Get-XblRemoteSandbox -MachineName $XboxName
    }

    return $Sandbox
}