3 Replies Latest reply on Oct 12, 2016 8:24 AM by scottydoo

    ePO web api and PowerShell


      Hey all,


      I'm trying to write some powershell to replace the curl.exe we currently rely on and I can't figure this out, I'm losing hair over this!



      curl -k -s -u admin:admin https://localhost:8443/remote/repoitory.checkInPackage?branch=Current -F file=@"C:\Package\Package.zip"



      $cred = Get-Credential #This is the admin username / pass

      Invoke-RestMethod -Uri "https://localhost:8443/remote/repoitory.checkInPackage?branch=Current" -Method POST -InFile "C:\Package\Package.zip" -ContentType "multipart/form-data" -Credential $cred

      cURL works and returns:



      PowerShell doesn't work and returns:

      The underlying connection was closed: An unexpected error occurred on a send.

      Any help would be appreciated.



      EDIT: Invoke-RestMethod -Uri "https://Server.domain.local:8443/remote/repoitory.checkInPackage?branch=Current" -Method POST -InFile "C:\Package\Package.zip" -ContentType "multipart/form-data" -Credential $cred


      Error 0 :

      Error processing multipart content

        • 1. Re: ePO web api and PowerShell



          maybe it's a certificate's problem.


          The parameter k for curl is telling the soft to not fail on nonvalid certificate.

          Can you try : http://stackoverflow.com/a/15841856 ?

          • 2. Re: ePO web api and PowerShell

            You can add this line to your powershell script to ignore certificate issues.


            [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 

            • 3. Re: ePO web api and PowerShell



              Did you figure this out in the end? This took me some time to figure out last year so in case you didn't get to the bottom of it, the reason this is failing is because you need to set a boundary. Here is the code I use I hope it helps:



              $ePOUser = "Admin"

              $ePOPass = "Password"

              $secCred = ConvertTo-SecureString $ePOPass -AsPlainText -Force

              $Creds = New-Object System.Management.Automation.PSCredential($ePOUser,$secCred)

              [String] $BaseURL = "https://ePOAddress`:PORT/remote/"   



              Function WebAPI($URL, $inFile)


                      $boundary_id = Get-Date -Format yyyyMMddhhmmssfffffff

                      $boundary = "------------------------------" + $boundary_id

                      start-sleep 5

                      If ($inFile -ne $NULL)


                          [System.Net.HttpWebRequest] $req = [System.Net.WebRequest]::create($URL)

                          $req.Method = "POST"

                          $req.ContentType = "multipart/form-data; boundary=$boundary"

                          $ContentLength = 0

                          $req.KeepAlive = $false


                          $req.Credentials = $Creds

                          $reqst = $req.getRequestStream()

                          <# Upload File#>

                          $FileBuffer = [System.IO.File]::ReadAllBytes("$inFile")

                          <# part-header #>

                          $header = "--$boundary`r`nContent-Disposition: form-data; name=`"file`"; filename=`"uploadfile`"`r`nContent-Type: text/xml`r`n`r`n"

                          $buffer = [Text.Encoding]::ascii.getbytes($header)      

                          $reqst.write($buffer, 0, $buffer.length)

                          $ContentLength = $ContentLength + $buffer.length



                          <# part-data #>

                          $reqst.write($FileBuffer, 0, $FileBuffer.length)

                          $ContentLength = $ContentLength + $FileBuffer.length


                          <# part-separator "One CRLF" #>

                          $terminal = "`r`n"

                          $buffer = [Text.Encoding]::ascii.getbytes($terminal)      

                          $reqst.write($buffer, 0, $buffer.length)


                          $terminal = "--$boundary--"

                          $buffer = [Text.Encoding]::ascii.getbytes($terminal)      

                          $reqst.write($buffer, 0, $buffer.length)

                          $ContentLength = $ContentLength + $buffer.length





                          [net.httpWebResponse] $res = $req.getResponse()



                          # Get results

                          $resstream = $res.GetResponseStream()

                          $sr = New-Object System.IO.StreamReader $resstream

                          $retval = $sr.ReadToEnd()

                          $results = $retval


                          $results = $NULL   





                          $Results = Invoke-RestMethod -Uri $URL -Method Post -Credential $Creds



                      $inFile = $NULL




              $Packages = Get-ChildItem("C:\Packages\*.zip")

              ForEach($Package in $Packages)


                  $WebRequest = $BaseURL + "repository.checkInPackage?packageLocation=$Package&branch=current"

                  $RetVal = WebAPI $WebRequest

                  Start-Sleep 10

                  If ($Retval.Contains("OK:`r`ntrue"))


                      Write-Output($Package.Name + " successfully imported.")




                      Write-Output("Failed to import " + $Package.Name)





              EDIT: Looking back at the code I don't think the boundary is required for repository packages so this may not be your issue after all. I think I only needed this to import certain files such as permission sets:


                          $WebRequest = ($BaseURL + "core.importPermissionSets")

                          $FileName = $File

                          $RetVal = WebAPI $WebRequest $FileName