Send-RemoteItem.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
#Requires -Version 3 function Send-RemoteItem { <# .SYNOPSIS Uploads a file to the filesystem on the server or media library through a Sitecore PowerShell Extensions web service. .PARAMETER Path The source path of the item to upload on the client side. .PARAMETER Destination The destination path of the item on the server side. .PARAMETER RootPath The predefined directory in which the item will be uploaded to. This is simply a keyword that maps to a predefined location on the server side. When using this you can simply provide the file or media item name in the Destination parameter. .PARAMETER SkipUnpack The compressed archive should not be unpacked during the upload process. This is a dynamic parameter that appears when RootPath is 'Media'. .PARAMETER SkipExisting Any existing items should not be overwritten. .EXAMPLE The following uploads a file to the root application path. $session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore Get-Item -Path C:\temp\data.xml | Send-RemoteItem @props -RootPath App .EXAMPLE The following uploads the image to the media library with the specified name. $session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore Get-Item -Path C:\image.png | Send-RemoteItem -Session $session -RootPath Media -Destination "Images/image2.png" .EXAMPLE The following uploads the image to the media library updating the item with the specified Id.. $session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore Get-Item -Path C:\temp\cover.jpg | Send-RemoteItem -Session $session -Destination "{04DAD0FD-DB66-4070-881F-17264CA257E1}" .EXAMPLE The following uploads a file to the specified absolute path. $session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore Send-RemoteItem -Session $session -Path "C:\temp\data.xml" -Destination "C:\inetpub\wwwroot\Console\Website\upload\data1.xml" .EXAMPLE The following uploads a compressed archive to the the media library and skips unpacking. Compress-Archive -Path C:\temp\kittens -DestinationPath C:\temp\kittens.zip $session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore Get-Item -Path C:\temp\kittens.zip | Send-RemoteItem @props -RootPath Media -Destination "Images/" -SkipUnpack .LINK Receive-RemoteItem .LINK New-ScriptSession .LINK Stop-ScriptSession #> [CmdletBinding(DefaultParameterSetName='Uri')] param( [Parameter(Mandatory=$true, ParameterSetName='Session')] [ValidateNotNull()] [pscustomobject]$Session, [Parameter(ParameterSetName='Uri')] [Uri[]]$ConnectionUri, [Parameter(ParameterSetName='Uri')] [string]$Username, [Parameter(ParameterSetName='Uri')] [string]$Password, [Parameter(ParameterSetName='Uri')] [System.Management.Automation.PSCredential] $Credential, [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [Alias("FullName")] [string]$Path, [Parameter(ParameterSetName='Session')] [Parameter(ParameterSetName='Uri')] [ValidateSet("App", "Data", "Debug", "Index", "Layout", "Log", "Media", "Package", "Serialization", "Temp")] [ValidateNotNullOrEmpty()] [string]$RootPath, [string]$Destination ) dynamicparam { if ($RootPath -eq "Media") { $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $skipUnpackAttribute = New-Object System.Management.Automation.ParameterAttribute $skipUnpackAttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $skipUnpackAttributeCollection.Add($skipUnpackAttribute) $skipUnpackParam = New-Object System.Management.Automation.RuntimeDefinedParameter('SkipUnpack', [switch], $skipUnpackAttributeCollection) $paramDictionary.Add('SkipUnpack', $skipUnpackParam) $skipExistingAttribute = New-Object System.Management.Automation.ParameterAttribute $skipExistingAttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $skipExistingAttributeCollection.Add($skipExistingAttribute) $skipExistingParam = New-Object System.Management.Automation.RuntimeDefinedParameter('SkipExisting', [switch], $skipExistingAttributeCollection) $paramDictionary.Add('SkipExisting', $skipExistingParam) return $paramDictionary } } process { $mediaId = [guid]::Empty $isMediaItem = $RootPath -eq "Media" -or [guid]::TryParse($Destination, [ref]$mediaId) if(!$isMediaItem -and (!$RootPath -and ![System.IO.Path]::IsPathRooted($Destination))) { Write-Error -Message "RootPath is required when Destination is not fully qualified." -ErrorAction Stop } $Destination = $Destination.TrimEnd('\','/') $output = $Destination if($mediaId -eq [guid]::Empty) { $extension = [System.IO.Path]::GetExtension($Path) if(!$output.EndsWith($extension)) { if(!$output.EndsWith("/") -and !$output.EndsWith("\")) { $output += "/" } $output += [System.IO.Path]::GetFileName($Path) } } if($Session) { $Username = $Session.Username $Password = $Session.Password $SharedSecret = $Session.SharedSecret $Credential = $Session.Credential $UseDefaultCredentials = $Session.UseDefaultCredentials $ConnectionUri = $Session | ForEach-Object { $_.Connection.BaseUri } } $serviceUrl = "/-/script" if($isMediaItem) { $serviceUrl += "/media/master/" + $output + "/?" } else { $serviceUrl += "/file/" + $RootPath + "/?path=" + $output + "&" } if($PSBoundParameters.SkipUnpack.IsPresent) { $serviceUrl += "&skipunpack=true" } if($PSBoundParameters.SkipExisting.IsPresent) { $serviceUrl += "&skipexisting=true" } foreach($uri in $ConnectionUri) { # http://hostname/-/script/type/origin/location $url = $uri.AbsoluteUri.TrimEnd("/") + $serviceUrl Write-Verbose -Message "Preparing to upload local item to the remote url $($url)" Add-Type -AssemblyName System.Net.Http $handler = New-Object System.Net.Http.HttpClientHandler $handler.AutomaticDecompression = [System.Net.DecompressionMethods]::GZip -bor [System.Net.DecompressionMethods]::Deflate $client = New-Object -TypeName System.Net.Http.Httpclient $handler if(![string]::IsNullOrEmpty($SharedSecret)) { $token = New-Jwt -Algorithm 'HS256' -Issuer 'SPE Remoting' -Audience ($uri.GetLeftPart([System.UriPartial]::Authority)) -Name $Username -SecretKey $SharedSecret -ValidforSeconds 30 $client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $token) } else { $authBytes = [System.Text.Encoding]::GetEncoding("iso-8859-1").GetBytes("$($Username):$($Password)") $client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", [System.Convert]::ToBase64String($authBytes)) } if($Credential) { $client.Credentials = $Credential } if($UseDefaultCredentials) { $client.UseDefaultCredentials = $UseDefaultCredentials } try { Write-Verbose -Message "Uploading $($Path)" [System.Net.HttpWebResponse]$errorResponse = $null; $fileStream = ([System.IO.FileInfo] (Get-Item -Path $Path)).OpenRead() $content = New-Object System.Net.Http.StreamContent($fileStream) $responseMessage = $client.PostAsync($url, $content).Result $fileStream.Close() $fileStream.Dispose() if(!$responseMessage.IsSuccessStatusCode) { Write-Error "Upload failed. $($responseMessage.ReasonPhrase)" return } Write-Verbose -Message "Upload complete." } catch [System.Net.WebException] { [System.Net.WebException]$script:ex = $_.Exception [System.Net.HttpWebResponse]$script:errorResponse = $ex.Response Write-Verbose -Message "Response exception message: $($ex.Message)" Write-Verbose -Message "Response status description: $($errorResponse.StatusDescription)" if($errorResponse.StatusCode -eq [System.Net.HttpStatusCode]::Forbidden) { Write-Verbose -Message "Check that the proper credentials are provided and that the service configurations are enabled." } elseif ($errorResponse.StatusCode -eq [System.Net.HttpStatusCode]::NotFound){ Write-Verbose -Message "Check that the service files exist and are properly configured." } } if($errorResponse){ Write-Error -Message "Server response: $($errorResponse.StatusDescription)" -Category ConnectionError ` -CategoryActivity "Upload" -CategoryTargetName $uri -Exception ($script:ex) -CategoryReason "$($errorResponse.StatusCode)" -CategoryTargetType $RootPath } } } } |