SpaceX.psm1

function Get-SXApi
{
    <#
    .SYNOPSIS
    Retrieve SpaceX API data
     
    .DESCRIPTION
    Retrieve SpaceX API data
     
    .EXAMPLE
    C:\PS> Get-SXApi
    Gets the API data
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

[CmdletBinding()]
PARAM()
    Get-SXData
}
function Get-SXCapsule {
    <#
    .SYNOPSIS
    Retrieve SpaceX capsule data
     
    .DESCRIPTION
    Retrieve SpaceX capsule data
     
    .PARAMETER Capsule
    The serial number of a specific capsule
     
    .EXAMPLE
    C:\PS> Get-SXCapsule
    Gets data about all the capsules
 
    .EXAMPLE
    C:\PS> Get-SXCapsule -Capsule C109
    Gets data about the capsule with the specified serial
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

    [CmdletBinding()]
    PARAM($Capsule)
    try {
        if ($Capsule) {
            $Splat = @{
                Path = "capsules/$Capsule"
            }
        }
        else {
            $Splat = @{
                Path = "capsules"
            }
        }
        (Get-SXData @Splat)
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
function Get-SXCompany
{
    <#
    .SYNOPSIS
    Retrieve SpaceX Company data
     
    .DESCRIPTION
    Retrieve SpaceX Company data
     
    .EXAMPLE
    C:\PS> Get-SXCompany
    Gets the company data
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

[CmdletBinding()]
PARAM()
    Get-SXData -Path info
}
function Get-SXCore {
  <#
  .SYNOPSIS
  Retrieve SpaceX Core data
   
  .DESCRIPTION
  Retrieve SpaceX Core data
 
  .PARAMETER Serial
  Specify the Core Serial
   
  .EXAMPLE
  C:\PS> Get-SXCore
  Gets data about all the cores
 
  .EXAMPLE
  C:\PS> Get-SXCore -Serial B1032
  Gets data about the core with the specified serial
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM($Serial)
  try{
      if($Serial)
      {
          $Splat = @{
              Path = "cores/$Serial"
          }
      }
      else{
          $Splat = @{
              Path = "cores"
          }
      }

      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXDragon {
  <#
  .SYNOPSIS
  Retrieve SpaceX Dragon Capsule data
   
  .DESCRIPTION
  Retrieve SpaceX Dragon Capsule data
 
  .PARAMETER ID
  Specify the Dragon Capsule ID
   
  .EXAMPLE
  C:\PS> Get-SXDragon
  Gets data about all the dragons
 
  .EXAMPLE
  Get-SXDragon -ID dragon1
  Gets data about the dragon with the specified ID
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM($ID)
  try{
      if($ID)
      {
          $Splat = @{
              Path = "dragons/$ID"
          }
      }
      else{
          $Splat = @{
              Path = "dragons"
          }
      }

      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXHistory {
  <#
  .SYNOPSIS
  Retrieve SpaceX historical events
   
  .DESCRIPTION
  Retrieve SpaceX historical events
 
  .PARAMETER ID
  Specify the historical event ID
   
  .EXAMPLE
  C:\PS> Get-SXHistory
  Gets all the history data
 
  .EXAMPLE
  C:\PS> Get-SXHistory -ID 1
  Gets data for the history event with the specified ID
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM($ID)
  try{
      if($ID)
      {
          $Splat = @{
              Path = "history/$ID"
          }
      }
      else{
          $Splat = @{
              Path = "history"
          }
      }

      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXLaunch {
    <#
    .SYNOPSIS
    Retrieve SpaceX launch data
     
    .DESCRIPTION
    Retrieve SpaceX launch data
     
    .PARAMETER Latest
    Just get data for the latest launch
     
    .PARAMETER Upcoming
    Just get data for upcoming launches
     
    .EXAMPLE
    Get-SXLaunch
    Gets data for all the launches
 
    .EXAMPLE
    Get-SXLaunch -Latest
    Gets data for the latest launch
 
    .EXAMPLE
    Get-SXLaunch -Upcoming
    Gets data for upcoming launches
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

    [CmdletBinding(DefaultParameterSetName='Default')]
    PARAM(
        [Parameter(ParameterSetName='Latest')][switch]$Latest,
        [Parameter(ParameterSetName='Upcoming')][switch]$Upcoming)
    try {
        if ($Latest) {
            $Splat = @{
                Path = "launches/latest"
            }
        }
        elseif ($Upcoming) {
            $Splat = @{
                Path = "launches/upcoming"
            }
        }

        else {
            $Splat = @{
                Path = "launches"
            }
        }

        (Get-SXData @Splat)
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
function Get-SXLaunchpad {
    <#
    .SYNOPSIS
    Retrieve SpaceX launch sites data
     
    .DESCRIPTION
    Retrieve SpaceX launch sites data
     
    .PARAMETER LaunchPad
    The Site ID of a specific launch pad
     
    .EXAMPLE
    C:\PS> Get-SXLaunchpad
    Gets data about all the launch pads
 
    .EXAMPLE
    C:\PS> Get-SXLaunchpad -Launchpad stls
    Gets data for the launch pad with the specified Site ID
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

    [CmdletBinding()]
    PARAM($LaunchPad)
    try {
        if ($LaunchPad) {
            $Splat = @{
                Path = "launchpads/$LaunchPad"
            }
        }
        else {
            $Splat = @{
                Path = "launchpads"
            }
        }

        (Get-SXData @Splat)
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
function Get-SXMission {
    <#
    .SYNOPSIS
    Retrieve SpaceX mission data
     
    .DESCRIPTION
    Retrieve SpaceX mission data
 
    .PARAMETER Mission
    Specify the mission ID
     
    .EXAMPLE
    C:\PS> Get-SXMission
    Gets data for all the missions
 
    .EXAMPLE
    C:\PS> Get-SXMission -Mission F3364BF
    Gets data for the mission with the specified Mission ID
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

    [CmdletBinding()]
    PARAM($Mission)
    try{
        if($Mission)
        {
            $Splat = @{
                Path = "missions/$Mission"
            }
        }
        else{
            $Splat = @{
                Path = "missions"
            }
        }

        (Get-SXData @Splat)
    }
    catch{
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
function Get-SXPayload {
  <#
  .SYNOPSIS
  Retrieve all past and upcoming SpaceX payload launches
   
  .DESCRIPTION
  Retrieve all past and upcoming SpaceX payload launches
 
  .PARAMETER PayloadID
  Specify the Payload ID
   
  .EXAMPLE
  C:\PS> Get-SXPayload
  Gets data about all the payloads
 
  .EXAMPLE
  C:\PS> Get-SXPayload -PayloadID "CRS-19"
  Gets data for the payload with the specified ID
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM($PayloadID)
  try{
      if($PayloadID)
      {
          $Splat = @{
              Path = "payloads/$PayloadID"
          }
      }
      else{
          $Splat = @{
              Path = "payloads"
          }
      }

      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXRoadster {
  <#
  .SYNOPSIS
  Retrieve current SpaceX Roadster orbit data
   
  .DESCRIPTION
  Retrieve current SpaceX Roadster orbit data
 
  .EXAMPLE
  Get-SXRoadster
  Gets data about the roadster
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM()
  try{
      $Splat = @{
          Path = "roadster"
      }
      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXRocket {
    <#
    .SYNOPSIS
    Retrieve SpaceX rocket data
     
    .DESCRIPTION
    Retrieve SpaceX rocket data
 
    .PARAMETER Rocket
    Specify the rocket name
     
    .EXAMPLE
    Get-SXRocket
    Gets data about all the rockets
 
    .EXAMPLE
    Get-SXRocket -Rocket falconheavy
    Gets data about the rocket with the specified Rocket ID
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

    [CmdletBinding()]
    PARAM($Rocket)
    try{
        if($Rocket)
        {
            $Splat = @{
                Path = "rockets/$Rocket"
            }
        }
        else{
            $Splat = @{
                Path = "rockets"
            }
        }

        (Get-SXData @Splat)
    }
    catch{
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
function Get-SXShip {
  <#
  .SYNOPSIS
  Retrieve SpaceX ship data
   
  .DESCRIPTION
  Retrieve SpaceX ship data (ASDS drone ships, tugs, fairing recovery ships, and various support ships)
 
  .PARAMETER ShipID
  Specify the ship ID
   
  .EXAMPLE
  C:\PS> Get-SXShip
  Gets data about all ships
 
  .EXAMPLE
  C:\PS> Get-SXShip -ShipID RACHEL
  Gets data aobut the ship with the specified Ship ID
   
  .NOTES
  https://github.com/lazywinadmin/SpaceX
  #>

  [CmdletBinding()]
  PARAM($ShipID)
  try{
      if($ShipID)
      {
          $Splat = @{
              Path = "ships/$ShipID"
          }
      }
      else{
          $Splat = @{
              Path = "ships"
          }
      }

      (Get-SXData @Splat)
  }
  catch{
      $PSCmdlet.ThrowTerminatingError($_)
  }
}
function Get-SXData
{
    <#
    .SYNOPSIS
    Retrieve data from the SpaceX API
     
    .DESCRIPTION
    Retrieve data from the SpaceX API. When the module was initialised,
    tests were performed to see whether TLS v1.2 needs to be manually
    enabled. If so, then it is enabled before making the call and reset
    again afterwards.
     
    .PARAMETER Path
    The path to the specific API being queried
     
    .EXAMPLE
    Get-SXData -Path missions
    Invokes the URL https://api.spacexdata.com/v3/missions
     
    .NOTES
    https://github.com/lazywinadmin/SpaceX
    #>

[CmdletBinding()]
PARAM($Path)
    If ($script:SecurityProtocolTweakRequired) {
        $originalSecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
        
        [Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12)
        Write-Verbose "Temporarily changing SecurityProtocol from [$originalSecurityProtocol] to [$([Net.ServicePointManager]::SecurityProtocol)]"
    }
    
    Try {
        Invoke-RestMethod -Uri https://api.spacexdata.com/v3/$Path
    }
    
    Finally {
        If ($script:SecurityProtocolTweakRequired) {
            [Net.ServicePointManager]::SecurityProtocol = $originalSecurityProtocol
            Write-Verbose "Resetting SecurityProtocol back to [$([Net.ServicePointManager]::SecurityProtocol)]"
        }
    }
} 
# Test to see if we can query the API now, or if we need to specifically
# tell PowerShell to allow TLS v1.2 (minimum supported by SpaceX API)
$SecurityProtocolTweakRequired = $False
TRY
{
    Get-SXApi|Out-Null
    # If no exception, then it must have worked - carry on"
}
CATCH [System.Net.WebException]
{
    # First attempt failed - enable TLS v1.2 and try again"
    $script:SecurityProtocolTweakRequired = $true

    TRY
    {
        Get-SXApi|Out-Null
        # If no exception, then it must have worked this time - carry on"
    }
    CATCH [System.Net.WebException]
    {
        # Still got an exception - must be another issue, re-raise it"
        throw
    }
}