Advertisement
Nixi80
Nov 3rd, 2022
25
0
Never
This is comment for paste Execute commands on AS400 from PS
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Good Evening, I have a problem with this file.
  2.  
  3. <#
  4. The whole reason this works is because IBM implemented a stored proecure called
  5. QCMDEXC which executes commands on the AS400 via SQL
  6.  
  7. The query has to be prepared in a certain way:
  8.  
  9. call qsys.qcmdexc('command',0000000007.00000)
  10.  
  11. The main thing is the second argument passed; which is the length of the command string.
  12. It has to be in this (15,5) format with all the leading and trailing zeros... whatever.
  13.  
  14. Also since this is, after all, an AS400 command wrapped in a SQL statement any single quotes
  15. inside the command string have to be esacped like so: 'derp' --> ''derp''
  16. #>
  17. function Build-Query([String] $command) {
  18. #Build first chunk of query, replaces any single quotes in command string with ''
  19. $query = "CALL QSYS2.QCMDEXC('" + ($command -replace "'","''") + "',"
  20.  
  21. #This bit builds the command length portion of the query
  22. $len1 = [String]$command.length #Lets say $command.length = 93
  23. $len2 = "0"*(10-$len1.length) #93 is 2 characters long so we need to pad 93 with (10-2) zeros to the left
  24. $len = $len2 + $len1 + ".00000" #Puts it all together along with the trailing .00000
  25.  
  26. $query = $query + $len + ")"
  27. echo $query
  28. }
  29.  
  30. function AS400-Command([String] $command) {
  31. $query = Build-Query $command
  32.  
  33. #Open connection to AS400
  34. $conn = New-Object System.Data.Odbc.OdbcConnection
  35. $conn.ConnectionString ="Provider=MSDASQL.1;DRIVER={Client Access ODBC Driver (32-bit)};SYSTEM=192.168.65.233;UID=M1PROD; PWD=M1PROD;
  36. DBQ=QGPL MPE_EURO MPE_GPEURO MPE_GPMEX MPE_GPUSA MPEGIPSER;DFTPKGLIB=QGPL;LANGUAGEID=ENU;QRYSTGLMT=-1;TRANSLATE=1;"
  37. $conn.Open()
  38.  
  39. $cmd = new-object System.Data.Odbc.OdbcCommand($query,$conn)
  40. $cmd.ExecuteNonQuery()
  41. Write-Output $query
  42. $conn.Close()
  43. }
  44.  
  45. AS400-Command "DLTSPLF FILE(*SELECT) SELECT(M1PROD)"
  46.  
  47.  
  48. The Error is the Following:
  49.  
  50. Eccezione durante la chiamata di "ExecuteNonQuery" con "0" argomento/i: "Overflow di un'operazione aritmetica."
  51. In riga:38 car:5
  52. + $cmd.ExecuteNonQuery()
  53. + ~~~~~~~~~~~~~~~~~~~~~~
  54. + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  55. + FullyQualifiedErrorId : OverflowException
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement