ps1.asciidoc 1.6 KB

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