cancel
Showing results for 
Search instead for 
Did you mean: 
theblackmini
Level 7

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:

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

PowerShell:

$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:

OK:

true


PowerShell doesn't work and returns:

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


Any help would be appreciated.

TheBlackMini

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

Returns:

Error 0 :

Error processing multipart content

0 Kudos
3 Replies
sammydia
Level 7

Re: ePO web api and PowerShell

Hi,

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 ?

0 Kudos
cspears
Level 7

Re: ePO web api and PowerShell

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

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

0 Kudos
scottydoo
Level 8

Re: ePO web api and PowerShell

Hi,

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`Smiley TongueORT/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.Timeout.Infinite

            $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

            $reqst.flush()

            $reqst.close()

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

            # Get results

            $resstream = $res.GetResponseStream()

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

            $retval = $sr.ReadToEnd()

            $results = $retval

            $results.Trim()

            $results = $NULL   

  

        }

        Else

        {

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

            $results.ToString()

        }

        $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.")

    }

    Else

    {

        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

0 Kudos