Advertisement
Guest User

Execute commands on AS400 from PS

a guest
Mar 3rd, 2016
2,872
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. The whole reason this works is because IBM implemented a stored proecure called
  3. QCMDEXC which executes commands on the AS400 via SQL
  4.  
  5. The query has to be prepared in a certain way:
  6.  
  7. call qsys.qcmdexc('command',0000000007.00000)
  8.  
  9. The main thing is the second argument passed; which is the length of the command string.
  10. It has to be in this (15,5) format with all the leading and trailing zeros... whatever.
  11.  
  12. Also since this is, after all, an AS400 command wrapped in a SQL statement any single quotes
  13. inside the command string have to be esacped like so: 'derp' --> ''derp''
  14. #>
  15. function Build-Query([String] $command) {
  16.     #Build first chunk of query, replaces any single quotes in command string with ''
  17.     $query = "call qsys.qcmdexc('" + ($command -replace "'","''") + "',"
  18.    
  19.     #This bit builds the command length portion of the query
  20.     $len1 = [String]$command.length    #Lets say $command.length = 93
  21.     $len2 = "0"*(10-$len1.length)      #93 is 2 characters long so we need to pad 93 with (10-2) zeros to the left
  22.     $len = $len2 + $len1 + ".00000"    #Puts it all together along with the trailing .00000
  23.    
  24.     $query = $query + $len + ")"
  25.     echo $query
  26. }
  27.  
  28. function AS400-Command([String] $command) {
  29.     $query = Build-Query $command
  30.    
  31.     #Open connection to AS400
  32.     $conn = New-Object System.Data.Odbc.OdbcConnection
  33.     $conn.ConnectionString = "Driver={iSeries Access ODBC Driver};System=AS400;Uid=USER;Pwd=PASS;"
  34.     $conn.Open()
  35.    
  36.     $cmd = new-object System.Data.Odbc.OdbcCommand($query,$conn)
  37.     $cmd.ExecuteNonQuery()
  38.    
  39.     $conn.Close()
  40. }
  41.  
  42. AS400-Command "FNDSTRPDM STRING('DERP') FILE(TSTLIB/QRPGLESRC) MBR(*ALL) OPTION(*NONE) PRTMBRLIST(*YES)"
Advertisement
Comments
  • Nixi80
    1 year
    # text 2.21 KB | 0 0
    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
Add Comment
Please, Sign In to add comment
Advertisement