ps1.asciidoc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. [role="xpack"]
  2. [[sql-client-apps-ps1]]
  3. === Microsoft PowerShell
  4. You can use the {es} ODBC driver to access {es} data from Microsoft PowerShell.
  5. IMPORTANT: Elastic does not endorse, promote or provide support for this application; for native Elasticsearch integration in this product, please reach out to its vendor.
  6. ==== Prerequisites
  7. * https://docs.microsoft.com/en-us/powershell/[Microsoft PowerShell]
  8. * {es-sql} <<sql-odbc, ODBC driver>>
  9. * A preconfigured User or System DSN (see <<dsn-configuration,Configuration>> section on how to configure a DSN).
  10. ==== Writing a script
  11. While putting the following instructions into a script file is not an absolute requirement, doing so will make it easier to extend and
  12. reuse. The following instructions exemplify how to execute a simple SELECT query from an existing index in your {es} instance, using a DSN
  13. configured in advance. Open a new file, `select.ps1`, and place the following instructions in it:
  14. ["source","powershell",subs="attributes,callouts"]
  15. --------------------------------------------
  16. $connectstring = "DSN=Local Elasticsearch;"
  17. $sql = "SELECT * FROM library"
  18. $conn = New-Object System.Data.Odbc.OdbcConnection($connectstring)
  19. $conn.open()
  20. $cmd = New-Object system.Data.Odbc.OdbcCommand($sql,$conn)
  21. $da = New-Object system.Data.Odbc.OdbcDataAdapter($cmd)
  22. $dt = New-Object system.Data.datatable
  23. $null = $da.fill($dt)
  24. $conn.close()
  25. $dt
  26. --------------------------------------------
  27. Now open a PowerShell shell and simply execute the script:
  28. [[apps_excel_exed]]
  29. .Run SQL in PowerShell
  30. image:images/sql/odbc/apps_ps_exed.png[]
  31. // vim: set noet fenc=utf-8 ff=dos sts=0 sw=4 ts=4 tw=138 columns=140