Param( [string]$oldvCOServer="localhost", [string]$oldvCOUser="vcoadmin", [string]$oldvCOPass="vcoadmin", [string]$oldvCOCategory="MyOldWorkflows", [string]$newvCOServer="localhost", [string]$newvCOUser="vcoadmin", [string]$newvCOPass="vcoadmin", [string]$newvCOCategory="MyWorkflows", [string]$TempFolder ) # vCO Port $vcoPort="8281" # Type to handle self-signed certificates add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [byte[]]$CRLF = 13, 10 function Get-AsciiBytes([String] $str) { return [System.Text.Encoding]::ASCII.GetBytes($str) } function ConvertTo-Base64($string) { $bytes = [System.Text.Encoding]::UTF8.GetBytes($string); $encoded = [System.Convert]::ToBase64String($bytes); return $encoded; } clear ############## EXPORT OLD WORKFLOWS ############################## Write-Host "Beginning Export Routine" -ForegroundColor Black -BackgroundColor Yellow Write-Host "" # build uri $uri = "https://"+$oldvCOServer+":"+$vCOPort+"/vco/api/workflows/?conditions=categoryName="+$oldvCOCategory # Authentication token or old server $token = ConvertTo-Base64("$($oldvCOUser):$($oldvCOPass)"); $auth = "Basic $($token)"; $header = @{"Authorization"= $auth; }; #execute API call $workflows = Invoke-RestMethod -URi $uri -Method Get -ContentType "application/xml" -Headers $header Write-Host "Exporting $($workflows.total) workflows from $oldvCOCategory" Write-Host "-------------------------------------------------------------------------------------------" #loop through each workflow and export to TempFolder foreach ($href in $workflows.link.href) { #retrieve information about the specific workflow $header = @{"Authorization"= $auth; }; $workflow = Invoke-RestMethod -URi $href -Method Get -ContentType "application/xml" -Headers $header #replace all spaces in workflow name $workflowname = [System.Text.RegularExpressions.Regex]::Replace($($workflow.name),"[^1-9a-zA-Z_]","") $filename = $TempFolder + $workflowname + ".workflow" # setup new header $header = @{"Authorization"= $auth; "Accept"="application/zip"; }; Write-Host "Exporting $($workflow.name) to $filename - " -NoNewline Invoke-RestMethod -URi $href -Method Get -ContentType "application/xml" -Headers $header -OutFile $filename Write-Host "Done" -ForegroundColor Green } Write-Host "" Write-Host "Export Routine Complete" -ForegroundColor Black -BackgroundColor Yellow ################################################################## ############## IMPORT WORKFLOWS ############################## Write-Host "" Write-Host "" Write-Host "Import Routines to new server" -ForegroundColor Black -BackgroundColor Yellow Write-Host "" #Generate auth for new vCO Server $token = ConvertTo-Base64("$($newvCOUser):$($newVCOPass)"); $auth = "Basic $($token)"; #Get Category ID $header = @{"Authorization"= $auth; }; $uri = "https://"+$newvCOServer+":"+$vCOPort+"/vco/api/categories/" $categories = Invoke-RestMethod -URi $uri -Method Get -Headers $header -ContentType "application/xml" foreach ($att in $categories.link) { if ($att.attributes.value -eq "HPEDSB") { foreach ($newatt in $att.attributes ) { if ($newatt.name -eq "id") { $categoryID = $newatt.value } } } } $impUrl = "https://$($newvCOServer):$($vcoPort)/vco/api/workflows?categoryId=$($categoryId)&overwrite=true"; $header = @{"Authorization"= $auth; "Accept"= "application/zip"; "Accept-Encoding"= "gzip,deflate,sdch";}; $workflows = Get-ChildItem $TempFolder -Filter *.workflow Write-Host "Importing $($workflows.count) workflows to $newvCOCategory" Write-Host "-------------------------------------------------------------------------------------------" foreach ($workflow in $workflows) { Write-Host "Importing $($workflow.name) - " -NoNewline $body = New-Object System.IO.MemoryStream $boundary = [Guid]::NewGuid().ToString().Replace('-','') $ContentType = 'multipart/form-data; boundary=' + $boundary $b2 = Get-AsciiBytes ('--' + $boundary) $body.Write($b2, 0, $b2.Length) $body.Write($CRLF, 0, $CRLF.Length) $b = (Get-AsciiBytes ('Content-Disposition: form-data; name="categoryId"')) $body.Write($b, 0, $b.Length) $body.Write($CRLF, 0, $CRLF.Length) $body.Write($CRLF, 0, $CRLF.Length) $b = (Get-AsciiBytes $categoryId) $body.Write($b, 0, $b.Length) $body.Write($CRLF, 0, $CRLF.Length) $body.Write($b2, 0, $b2.Length) $body.Write($CRLF, 0, $CRLF.Length) $b = (Get-AsciiBytes ('Content-Disposition: form-data; name="file"; filename="$($workflow.Name)";')) $body.Write($b, 0, $b.Length) $body.Write($CRLF, 0, $CRLF.Length) $b = (Get-AsciiBytes 'Content-Type:application/octet-stream') $body.Write($b, 0, $b.Length) $body.Write($CRLF, 0, $CRLF.Length) $body.Write($CRLF, 0, $CRLF.Length) $b = [System.IO.File]::ReadAllBytes($workflow.FullName) $body.Write($b, 0, $b.Length) $body.Write($CRLF, 0, $CRLF.Length) $body.Write($b2, 0, $b2.Length) $b = (Get-AsciiBytes '--'); $body.Write($b, 0, $b.Length); $body.Write($CRLF, 0, $CRLF.Length); $header = @{"Authorization"= $auth; "Accept"= "application/zip"; "Accept-Encoding"= "gzip,deflate,sdch";}; Invoke-RestMethod -Method POST -Uri $impUrl -ContentType $ContentType -Body $body.ToArray() -Headers $header Write-Host "Done" -foregroundcolor Green } Write-Host "" Write-Host "Import Routine complete" -ForegroundColor Black -BackgroundColor Yellow ##################################################################