Byshop303

QueryCarStatus.ps1

Oct 30th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ErrorActionPreference = "Stop" #Cause the script to stop if any errors are detected. Sometimes the API call is a bit wonky so if the script fails just run it again till it succeeds. Should take no more than 3-4 times tops.
  2.  
  3. #Put your username/password here
  4.  
  5. $MyUserName = "TypeYourUserNameHereBetweenTheQuotes"
  6. $MyPassword = "TypeYourPasswordHereBetweenTheQuotes"
  7.  
  8. #Reset the "Fianl Output" variable each time the script is run. This is related to displaying all of the corked/uncorked data for a list of vehicles at the end of the output.
  9.  
  10. $FinalOutput = ""
  11.  
  12. #Check for presense of Tesla API Powershell Module
  13.  
  14. if((get-module -name Tesla) -eq $null)
  15. {
  16.     write-output "Tesla Powershell Module not installed. Please download and install ths module from: https://github.com/andylyonette/TeslaPSModule and re-run this script"  
  17. }
  18.  
  19. #Check to see if a Token was already created this session (i.e. you are re-running the script without closing the Powershell session). Reuse the existing token if there is one.
  20.  
  21. if($Token -eq $null)
  22. {
  23.  
  24.     #Create a secure credential object using the above stored username/password.
  25.  
  26.     $SecPasswd = ConvertTo-SecureString $MyPassword -AsPlainText -Force
  27.  
  28.     $MyCreds = New-Object System.Management.Automation.PSCredential ($MyUserName, $SecPasswd)
  29.  
  30.     #Get the bearer token to use for every command that queries the API for the rest of the script.
  31.  
  32.     $Token = Get-TeslaToken -Credential ($MyCreds)
  33. }
  34.  
  35. #Retreive information about current vehicle. Script is written on the assumption of one vehicle will be returned per set of login credentials and will fail if there's more than one, but this can be easily modified to work for multiple cars.
  36.  
  37. $Vehicles = Get-TeslaVehicles -Token $Token
  38.  
  39. foreach($Vehicle in $Vehicles)
  40. {
  41.  
  42.     #Get the vehicle summary data
  43.  
  44.     $VehicleSummaryOutput = Get-TeslaVehicleSummary $Vehicle -Token $Token
  45.  
  46.     #Display the vehicle summary data onscreen.
  47.  
  48.     $VehicleSummaryOutput.vehicle_state
  49.  
  50.     #Check the perf_config flag to see if it matched currently known values for "uncorked" cars.
  51.  
  52.     if($VehicleSummaryOutput.vehicle_state.perf_config -eq "P3")
  53.         {
  54.             #Inform the user that the vehicle is corked. Store the sentence into an output variable to be displayed at the end of the script.
  55.  
  56.             $FinalOutput += "$($VehicleSummaryOutput.display_name)'s performance configuration is ""corked"". Your performance configuration flag is set to ""$($VehicleSummaryOutput.vehicle_state.perf_config)"".`n"
  57.         }
  58.         elseif(($VehicleSummaryOutput.vehicle_state.perf_config -eq "P1") -or ($VehicleSummaryOutput.vehicle_state.perf_config -eq "P2"))
  59.         {
  60.  
  61.             #Inform the user that the vehicle is uncorked. Store the sentence into an output variable to be displayed at the end of the script.
  62.  
  63.             $FinalOutput += "$($VehicleSummaryOutput.display_name)'s performance configuration is ""uncorked"" Your performance configuration flag is set to ""$($VehicleSummaryOutput.vehicle_state.perf_config)"".`n"
  64.         }
  65.         else
  66.         {
  67.            
  68.             #Inform the user that the vehicle's performance status is unknown because the value was something other than P1,P2, or P3. Store the sentence into an output variable to be displayed at the end of the script.
  69.            
  70.             $FinalOutput += "$($VehicleSummaryOutput.display_name)'s performance configuraion is unknown. The output of your performance configuration is ""$($VehicleSummaryOutput.vehicle_state.perf_config)"", which is not a value this script recognizes.`n"
  71.         }
  72. }
  73.  
  74. #Display corking status of all evaluated vehicles.
  75.  
  76. Write-Output $FinalOutput
Add Comment
Please, Sign In to add comment