<# Is this 'live' or a trial run? NOTE: The 'natural key' for an [eventListener] is its 'name' attribute. An eventListener will be updated (instead of inserted) if one with the same 'name' exists.#> $isDryRun = true <# about the file we are POSTing #> $xmlFileName = "{your partial or complete configuration xml file name, eg. cloverConfig.xml}"; $xmlPath = "{the path to your configuration xml file}\$xmlFileName" $xmlEnc = [IO.File]::ReadAllText($xmlPath, [System.Text.Encoding]::UTF8) <# Crete the boundary for the form-data POST parameters #> $timestamp = Get-Date -Format o <# or a GUID --> [System.Guid]::NewGuid().ToString() #> $boundary = ("----WebKitFormBoundary" + $timestamp) <# borrowed this prefix from PostMan! #> $prefixedBoundary = ("--" + $boundary) <# -ContentType #> $contentType = "multipart/form-data; boundary=$boundary" <# -Uri #> $uri = "{your clover server}:8080/clover/simpleHttpApi/import_server_config" <# -Headers Use a .Net Dictionary, or just use the PowerShell hashtable syntax, eg. @{"key1"="value1";"key2"="value2"} #> $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization", "{your Basic authorization string}") $headers.Add("Accept", "*/*") $headers.Add("Accept-Encoding", "gzip, deflate") $headers.Add("Cache-Control", "no-cache") $headers.Add("Content-Type", $contentType) $headers.Add("Accept-Language", "en-US,en;q=0.8") <# -Body PowerShell doesn't appear to natively support form-data with file upoads, so using System.Text.StringBuilder to compose the body. #> $bodyBuilder = New-Object System.Text.StringBuilder <# request parameter: "include" #> $bodyBuilder.AppendLine($prefixedBoundary) $bodyBuilder.AppendLine("Content-Disposition: form-data; name=`"include`"") $bodyBuilder.AppendLine() $bodyBuilder.AppendLine("eventListeners") <# request parameter: "newOnly" #> $bodyBuilder.AppendLine($prefixedBoundary) $bodyBuilder.AppendLine("Content-Disposition: form-data; name=`"newOnly`"") $bodyBuilder.AppendLine() $bodyBuilder.AppendLine("true") <# request parameter: "dryRun" #> $bodyBuilder.AppendLine($prefixedBoundary) $bodyBuilder.AppendLine("Content-Disposition: form-data; name=`"dryRun`"") $bodyBuilder.AppendLine() $bodyBuilder.AppendLine("$isDryRun") <# request parameter: "verbose" #> $bodyBuilder.AppendLine($prefixedBoundary) $bodyBuilder.AppendLine("Content-Disposition: form-data; name=`"verbose`"") $bodyBuilder.AppendLine() $bodyBuilder.AppendLine("FULL") <# request parameter: "xmlFile" #> $bodyBuilder.AppendLine($prefixedBoundary) $bodyBuilder.AppendLine("Content-Disposition: form-data; name=`"xmlFile`"; filename=`"$xmlFileName`"") <# filename is optiona #> $bodyBuilder.AppendLine("Content-Type: text/xml") $bodyBuilder.AppendLine() $bodyBuilder.AppendLine($xmlEnc) $bodyBuilder.AppendLine($prefixedBoundary + "--") $body = $bodyBuilder.ToString() try { <# tried to execute via Invoke-RestMethod; no luck #> $response = Invoke-WebRequest -Method POST -Uri $uri -ContentType $contentType -Headers $headers -Body $body <# for automated implementations, exiting with a non-zero value can be important #> if ($response.StatusCode -eq 200) { Write-Host $response.RawContent Exit 0 } else { Write-Warning $response.Content Exit $response.StatusCode } } catch [System.Net.WebException] { Write-Error($_ ) throw $_ }