Guest User

Untitled

a guest
Mar 22nd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. param($tnsnamesPath = 'c:\tns\tnsnames.ora',$username = 'user',$password = 'gotmehere', $connectionName = 'mustard', $query = 'Select sysdate from dual')
  2. $simplySQLPath = (Get-Module -ListAvailable simplySQL).ModuleBase
  3. if($simplySQLPath -and (test-path $tnsnamesPath -PathType Leaf) -and (![string]::IsNullOrEmpty($node)))
  4. {
  5. [System.Reflection.Assembly]::LoadFile("$simplySQLPath\DataReaderToPSObject.dll") | OUT-NULL
  6. Import-Module SimplySql -Force
  7. $parsedTN = (get-content $tnsnamesPath -raw) -replace '(.*\=.*|\n.*\=)(.*|\n.*)\(DESCRIPTION*.\=' ,'Data Source = (DESCRIPTION ='
  8. $splitTN = $parsedTN -split '(?=.*Data Source = \(DESCRIPTION \=)'
  9. $tnsnames = $splitTN |?{$_ -like "*$connectionName*"}
  10. $connstring = "$tnsnames;User Id=$username;Password=$password"
  11. try
  12. {
  13. Open-OracleConnection -ConnectionString $connstring -ConnectionName $connectionName
  14. $result = Invoke-SqlQuery -ConnectionName $connectionName -Query "$SQLQuery"
  15. Close-SqlConnection -ConnectionName $connectionName
  16. }
  17. catch
  18. {
  19. $_.exception
  20. }
  21.  
  22. }
  23. Else
  24. {
  25. if(!(test-path $tnsnamesPath -PathType Leaf -ErrorAction Ignore))
  26. {
  27. Throw "Check TNSnamesPath: $tnsNamesPath"
  28. }
  29. else
  30. {
  31. Throw "Exeception SIMPLYSQL not found in module Path $($env:PSModulePath)"
  32. }
  33. }
  34. $result
Add Comment
Please, Sign In to add comment