Send-Xbox.ps1

function Send-Xbox
{
    <#
    .Synopsis
        Sends simulated input to an Xbox 360 Controller
    .Description
        Sends simulated controller input through the debug channels.
        Input can be simulated for any xbox 360 control.
        When used together with the Start-Sleep cmdlet, this makes it possible
        for people with little programming background to automate a task with their xbox
    .Parameter Player
        The controller number of the player (1-4)
    .Parameter RightTrigger
        The percentage pulled the right trigger should be.
        0 is not at all pulled, 1 is fully pulled
    .Parameter LeftTrigger
        The percentage pulled the left trigger should be.
        0 is not at all pulled, 1 is fully pulled
    .Parameter LeftStickX
        A value from -1 to 1 indicating the Left Stick's X coordinate
        -1 is equivilent to having the stick over all of the way to the left
        1 is equivilent to having the stick over all of the way to the right
        0 is equivilent to putting the stick in the center
    .Parameter LeftStickY
        A value from -1 to 1 indicating the Left Stick's Y coordinate
        -1 is equivilent to having the stick over all of the way to the top
        1 is equivilent to having the stick over all of the way to the bottom
        0 is equivilent to putting the stick in the center
    .Parameter RightStickX
        A value from -1 to 1 indicating the Right Stick's X coordinate
        -1 is equivilent to having the stick over all of the way to the left
        1 is equivilent to having the stick over all of the way to the right
        0 is equivilent to putting the stick in the center
    .Parameter RightStickY
        A value from -1 to 1 indicating the Right Stick's Y coordinate
        -1 is equivilent to having the stick over all of the way to the top
        1 is equivilent to having the stick over all of the way to the bottom
        0 is equivilent to putting the stick in the center
    .Parameter DPadUp
        If set, simulates pressing the DPadUp button
    .Parameter DPadDown
        If set, simulates pressing the DPadDown button
    .Parameter DPadLeft
        If set, simulates pressing the DPadLeft button
    .Parameter DPadRight
        If set, simulates pressing the DPadRight button
    .Parameter Start
        If set, simulates pressing the Start button
    .Parameter Back
        If set, simulates pressing the back button
    .Parameter LeftThumb
        If set, simulates pressing the left stick button
    .Parameter RightThumb
        If set, simulates pressing the right stick button
    .Parameter LeftBumper
        If set, simulates pressing the left bumper button
    .Parameter RightBumper
        If set, simulates pressing the right bumper button
    .Parameter Bind
        If set, simulates pressing the bind button
    .Parameter XBox
        If set, simulates pressing the xbox button
    .Parameter A
        If set, simulates pressing the A button
    .Parameter B
        If set, simulates pressing the B button
    .Parameter X
        If set, simulates pressing the X button
    .Parameter Y
        If set, simulates pressing the Y button
    .Parameter For
        The time to amount of time to send the button.
        When you set the gamepad state, this is the amount of time that
        should pass before the gamepad state is cleared
    .Parameter Wait
        The amount of time to wait after the gamepad state is cleared.
        This is used to allow for the xbox to respond to the button before continuing
    .Example
        Send-Xbox -XBox
    #>

    [CmdletBinding(DefaultParameterSetName="Controls")]
    param(
        [Parameter()]
        [ValidateRange(1,4)]
        [int]$Player = 1,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(0,1)]
        [Double]$RightTrigger,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(0,1)]
        [Double]$LeftTrigger,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(-1,1)]
        [Double]$LeftStickX,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(-1,1)]
        [Double]$LeftStickY,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(-1,1)]
        [Double]$RightStickX,
        [Parameter(ParameterSetName="Controls")]
        [ValidateRange(-1,1)]
        [Double]$RightStickY,
        [Parameter(ParameterSetName="Controls")]
        [Timespan]$For = ([Timespan]"0:0:0.15"),
        [Parameter(ParameterSetName="Controls")]
        [Timespan]$Wait = ([Timespan]"0:0:0.05"),
        [Parameter(ParameterSetName="Controls")]
        [UInt32]$Repeat = 1,
        [Parameter(ParameterSetName="Controls")]
        [switch]$DPadUp,
        [Parameter(ParameterSetName="Controls")]
        [switch]$DPadDown,
        [Parameter(ParameterSetName="Controls")]
        [switch]$DPadLeft,
        [Parameter(ParameterSetName="Controls")]
        [switch]$DPadRight,
        [Parameter(ParameterSetName="Controls")]
        [switch]$Start,
        [Parameter(ParameterSetName="Controls")]
        [switch]$Back,
        [Parameter(ParameterSetName="Controls")]
        [switch]$LeftThumb,
        [Parameter(ParameterSetName="Controls")]
        [switch]$RightThumb,
        [Parameter(ParameterSetName="Controls")]
        [switch]$LeftBumper,
        [Parameter(ParameterSetName="Controls")]
        [switch]$RightBumper,
        [Parameter(ParameterSetName="Controls")]
        [switch]$XBox,
        [Parameter(ParameterSetName="Controls")]
        [switch]$Bind,
        [Parameter(ParameterSetName="Controls")]
        [switch]$A,
        [Parameter(ParameterSetName="Controls")]
        [switch]$B,
        [Parameter(ParameterSetName="Controls")]        
        [switch]$X,
        [Parameter(ParameterSetName="Controls")]        
        [switch]$Y,
        [Parameter(ParameterSetName="Controls")]        
        [switch]$DoNotStop,
        [Parameter(ParameterSetName="Gamepad",
            ValueFromPipeline=$true)]
        [Xap.XapController+DM_XINPUT_GAMEPAD]
        $Gamepad
       
    )
    
    process {
        if ($pscmdlet.ParameterSetName -eq "Gamepad") {
            [Xap.XapController]::SendGamepadInput($Player, $Gamepad)
            return 
        }
        if (-not $script:ControllerIsBound) {
            $script:ControllerIsBound = @{}
        }
        $null = $xbdm::DmAutomationBindController($Player - 1, 0)
        $null = $xbdm::DmAutomationConnectController($Player - 1)
        for ($n = 1; $n -le $Repeat; $n++) {
            $gamepad = New-Object XBDM.DM_XINPUT_GAMEPAD
            if ($psBoundParameters.RightTrigger) {
                $gamepad.RightTrigger = $RightTrigger*255
            }
            if ($psBoundParameters.LeftTrigger) {
                $gamepad.LeftTrigger = $LeftTrigger*255
            }
            if ($psBoundParameters.LeftStickX) {
                $gamepad.LeftThumbstickX = $LeftStickX*[int16]::MaxValue
            }
            if ($psBoundParameters.LeftStickY) {
                $gamepad.LeftThumbstickY = $LeftStickY*[int16]::MaxValue
            }
            if ($psBoundParameters.RightStickX) {
                $gamepad.RightThumbstickX = $RightStickX*[int16]::MaxValue
            }
            if ($psBoundParameters.RightStickY) {
                $gamepad.RightThumbstickY = $RightStickY*[int16]::MaxValue
            }                
            $buttonString = ""
            $psBoundParameters.GetEnumerator() | 
                Where-Object { $_.Value -is [switch] -and 
                    "Verbose", "DoNotStop" -notcontains $_.Key } |
                ForEach-Object {
                    $buttonString+="$($_.Key),"
                }
            if ($buttonString) {
                $gamePad.Button = $buttonString.Trim(",") -as [Xbdm.XboxButton]
            }
            $null  = [Xbdm.XbdmXboxObject]::DmAutomationSetGamepadState(
                $Player - 1 , 
                [ref]$gamepad) 
            if ($for.TotalMilliseconds) {
                Start-Sleep -Milliseconds $for.TotalMilliseconds
            }
            if (-not $DoNotStop) {
                $gamepad = New-Object XBDM.DM_XINPUT_GAMEPAD
                $null  = [Xbdm.XbdmXboxObject]::DmAutomationSetGamepadState(
                    $Player - 1 , 
                    [ref]$gamepad) 
            }
            if ($Wait.TotalMilliseconds) {
                Start-Sleep -Milliseconds $Wait.TotalMilliseconds
            }        
        }
    }
}