applications-ps1.asciidoc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[sql-odbc-applications-ps1]]
  4. === Microsoft PowerShell
  5. experimental[]
  6. [quote, https://docs.microsoft.com/en-us/powershell/scripting/powershell-scripting]
  7. ____
  8. https://docs.microsoft.com/en-us/powershell/[PowerShell] is a task-based command-line shell and scripting language built on .NET.
  9. ____
  10. PowerShell is available on all recent Windows Desktop OSes. It also has embedded ODBC support, thus offering a quick and accessible way to
  11. connect to {es}.
  12. ==== Prerequisites
  13. * Microsoft PowerShell
  14. * {product}
  15. * A preconfigured User or System DSN (see <<sql-odbc-setup>> section on how to configure a DSN).
  16. ==== Writing a script
  17. While putting the following instructions into a script file is not an absolute requirement, doing so will make it easier to extend and
  18. reuse. The following instructions exemplify how to execute a simple SELECT query from an existing index in your {es} instance, using a DSN
  19. configured in advance. Open a new file, `select.ps1`, and place the following instructions in it:
  20. ["source","powershell",subs="attributes,callouts"]
  21. --------------------------------------------
  22. $connectstring = "DSN=Local Elasticsearch;"
  23. $sql = "SELECT * FROM library"
  24. $conn = New-Object System.Data.Odbc.OdbcConnection($connectstring)
  25. $conn.open()
  26. $cmd = New-Object system.Data.Odbc.OdbcCommand($sql,$conn)
  27. $da = New-Object system.Data.Odbc.OdbcDataAdapter($cmd)
  28. $dt = New-Object system.Data.datatable
  29. $null = $da.fill($dt)
  30. $conn.close()
  31. $dt
  32. --------------------------------------------
  33. Now open a PowerShell shell and simply execute the script:
  34. [[apps_excel_exed]]
  35. .Run SQL in PowerShell
  36. image:images/sql/odbc/apps_ps_exed.png[]
  37. // vim: set noet fenc=utf-8 ff=dos sts=0 sw=4 ts=4 tw=138 columns=140